我試圖將文件下載到我的計算機,並在同一時間將其保存到字節數組:下載到文件並保存到字節數組
try
{
var req = (HttpWebRequest)HttpWebRequest.Create(url);
var fileStream = new FileStream(filePath,
FileMode.Create, FileAccess.Write, FileShare.Write);
using (var resp = req.GetResponse())
{
using (var stream = resp.GetResponseStream())
{
byte[] buffer = new byte[0x10000];
int len;
while ((len = stream.Read(buffer, 0, buffer.Length)) > 0)
{
//Do with the content whatever you want
// ***YOUR CODE***
MemoryStream memoryStream = new MemoryStream();
if (len > 0)
{
memoryStream.Write(buffer, 0, len);
len = stream.Read(buffer, 0, buffer.Length);
}
file = memoryStream.ToArray();
fileStream.Write(buffer, 0, len);
}
}
}
fileStream.Close();
}
catch (Exception exc) { }
而且我注意到,這不是下載所有文件這個。
我想這樣做,因爲我想下載一個文件,並在同一時間使用它。 任何想法爲什麼會發生這個問題?
代碼沒有任何意義...... –
此外,它似乎是一個您以前的問題的dup:http://stackoverflow.com/questions/15951621/download-a-file-and-read-from-it-in-the-same-time –
http://stackoverflow.com/questions/ 1278986 /下載-A-文件過HTTP - 進入 - 一個字節陣列式-C – Guy