0
我想上傳帶有http post的文件。下面的方法工作正常,但與文件> 1GB我得到OutOfMemoryExceptions
使用System.Net.WebClient上傳文件時出現OutOfMemoryException
我發現基於一些solutions上AllowWriteStreamBuffering
和System.Net.WebRequest
但似乎並沒有幫助,在這種情況下,因爲我需要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);
}
}
}
}
什麼我需要改變,以避免這個錯誤?
你有沒有使用[WebClient.UploadFileAsync]考慮(https://msdn.microsoft.com/en-us /library/ms144232(v=vs.110).aspx)? –
這些問題需要記錄安裝的反惡意軟件產品。並顯示啓用了非託管調試的堆棧跟蹤。 –