如何在IIS()中使用FileStream在WebService中上传文件(大容量)

2025-06-30 13:31:36 cctv5世界杯 8141

要在WebServices(不是)中上传一个大文件,请遵循以下方法:

在服务器端文件Web.Config中,添加此xml并根据上传的最大大小更改maxRequestLength的值,在本例中,maxRequestLength为4MB。

代码语言:javascript运行复制

executionTimeout="600"

maxRequestLength="8192"

useFullyQualifiedRedirectUrl="false"

minFreeThreads="8"

minLocalRequestFreeThreads="4"

appRequestQueueLimit="100"

enableVersionHeader="true"

/>在服务器端添加另一个公共函数,此函数接收到的byte[]包含文件、文件名和路径,您不会将文件保存在服务器中。

代码语言:javascript运行复制 public String UploadFile(byte[] fileByte, String fileName, String savePath)

{

string newPath = savePath;

if (!Directory.Exists(newPath))

{

Directory.CreateDirectory(newPath);

}

newPath = newPath + fileName;

System.IO.FileStream fs1 = null;

byte[] b1 = null;

b1 = fileByte;

fs1 = new FileStream(newPath, FileMode.Create);

fs1.Write(b1, 0, b1.Length);

fs1.Flush();

fs1.Close();

fs1 = null;

return newPath;

}在客户端,添加以下代码以发送文件:

代码语言:javascript运行复制Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();

dlg.FileName = "Select a File";

dlg.DefaultExt = ".xls";

dlg.Filter = "Excel documents (.xls)|*.xls";

// Show open file dialog box

Nullable result = dlg.ShowDialog();

// Process open file dialog box results

if (result == true)

{

string filename = dlg.FileName;

string file = Path.GetFileName(filename);

System.IO.FileStream fs1 = null;

fs1 = System.IO.File.Open(filename, FileMode.Open, FileAccess.Read);

byte[] b1 = new byte[fs1.Length];

fs1.Read(b1, 0, (int)fs1.Length);

fs1.Close();

String savePath = @"C:\DOC\IMPORT\";

String newPath = WEB_SERVICES.UploadFile(b1, file, savePath);

MessageBox.Show(String.Format("The file is uploaded in {0}", newPath));

}

else

{

MessageBox.Show("Select a file, please");

}

H5制作哪家强?四大H5页面制作工具大比拼
月息3厘竟不是年化利率3.6%?贷款利率那么多「套路」教你算懂贷款的利率