我想上传一篇带有http帖子的文件。以下方法工作正常但文件大于1GB我得到OutOfMemoryExceptions
我根据AllowWriteStreamBuffering
和System.Net.WebRequest
找到了一些solutions,但在这种情况下似乎没有帮助,因为我需要用System.Net.WebClient
来解决它。
抛出异常时我的应用程序的内存使用量总是大约~500MB
string file = @"C:\test.zip";
string url = @"http://foo.bar";
using (System.Net.WebClient client = new System.Net.WebClient())
{
using (System.IO.Stream fileStream = System.IO.File.OpenRead(file))
{
using (System.IO.Stream requestStream = client.OpenWrite(new Uri(url), "POST"))
{
byte[] buffer = new byte[16 * 1024];
int bytesRead;
while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) > 0)
{
requestStream.Write(buffer, 0, bytesRead);
}
}
}
}
我需要更改哪些内容才能避免此错误?
答案 0 :(得分:1)
经过一天的尝试,我找到了解决这个问题的方法。
也许这将有助于未来的访客
$ionicLoading.show();
var datacall=//your call to a data service function using $http to your backend
datacall.$promise.then(function(success){$ionicLoading.hide()},function(err){$ionicLoading.hide()});
扩展string file = @"C:\test.zip";
string url = @"http://foo.bar";
using (System.IO.Stream fileStream = System.IO.File.OpenRead(file))
{
using (ExtendedWebClient client = new ExtendedWebClient(fileStream.Length))
{
using (System.IO.Stream requestStream = client.OpenWrite(new Uri(url), "POST"))
{
byte[] buffer = new byte[16 * 1024];
int bytesRead;
while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) > 0)
{
requestStream.Write(buffer, 0, bytesRead);
}
}
}
}
方法
WebClient