我有一个简单的功能,可以通过网页上传文件。
当我上传大小为16 MB的文件时,它可以工作,但是如果我尝试使用更大的文件,例如。 57 MB,程序抛出
Stream was too long exception
。
Stream memStream = new MemoryStream();
for (int i = 0; i < files.Length; i++)
{
memStream.Write(boundarybytes, 0, boundarybytes.Length);
var header = string.Format(headerTemplate, "uplTheFile", files[i]);
var headerbytes = Encoding.UTF8.GetBytes(header);
memStream.Write(headerbytes, 0, headerbytes.Length);
using (var fileStream = new FileStream(files, FileMode.Open, FileAccess.Read))
{
var buffer = new byte[fileStream.Length];
var bytesRead = 0;
while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
{
memStream.Write(buffer, 0, bytesRead);
}
}
}
memStream.Write(endBoundaryBytes, 0, endBoundaryBytes.Length);
request.ContentLength = memStream.Length;
using (Stream requestStream = request.GetRequestStream())
{
memStream.Position = 0;
byte[] tempBuffer = new byte[memStream.Length];
memStream.Read(tempBuffer, 0, tempBuffer.Length);
memStream.Close();
requestStream.Write(tempBuffer, 0, tempBuffer.Length);
}
using (var response = request.GetResponse())
{
Stream stream2 = response.GetResponseStream();
StreamReader reader2 = new StreamReader(stream2);
return reader2.ReadToEnd();
}
我以这种方式初始化请求。
HttpWebRequest request = HttpWebRequest.Create(url);
request.Timeout = 3600000;
request.Method = "POST";
request.KeepAlive = true;
答案 0 :(得分:0)
那么您要连接的服务器有限制吗?在这种情况下,您无论如何都无法上传?
答案 1 :(得分:0)
for (int i = 0; i < files.Length; i++)
是问题所在。