【发布时间】:2014-05-27 10:57:14
【问题描述】:
我的要求是传输大小为 400MB 或更大的 zip 文件;以下代码至少适用于 40MB;但要获得更多,我必须将 byte[] bytes = new byte[50000000]; 更改为 byte[] bytes = new byte[400000000]; 并将 maxRequestLength 更改为 maxRequestLength="409600";
问题是byte[] bytes = new byte[100000000]; 返回有关空间不足的错误。那么如何使用 WebClient 传输大文件呢?
WebClient client = new WebClient();
client.AllowWriteStreamBuffering = true;
UriBuilder ub = new UriBuilder("http://localhost:57596/UploadImages.ashx");
ub.Query = "ImageName=" + "DataSet" + DataSetId + ".zip";
client.OpenWriteCompleted += (InputStream, eArguments) =>
{
try
{
using (Stream output = eArguments.Result)
{
output.Write(ImagesAux, 0, (int)ImagesAux.Length);
//numeroimagem++;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
//throw;
}
};
client.OpenWriteAsync(ub.Uri);
在 UploadImages.ashx 中
public void ProcessRequest(HttpContext context)
{
//context.Response.ContentType = "text/plain";
//context.Response.Write("Hello World");
string ImageName = context.Request.QueryString["ImageName"];
string UploadPath = context.Server.MapPath("~/ServerImages/");
using (FileStream stream = File.Create(UploadPath + ImageName))
{
byte[] bytes = new byte[50000000]; //
int bytesToRead = 0;
while ((bytesToRead =
context.Request.InputStream.Read(bytes, 0, bytes.Length)) != 0)
{
stream.Write(bytes, 0, bytesToRead);
stream.Close();
}
}
}
在 Web.config 中
<httpRuntime targetFramework="4.5" maxRequestLength="40960"/>
【问题讨论】:
-
bytes[500000] 只是我会保持较低的缓冲区大小,不要在每次写入时关闭流。
标签: c# silverlight webclient