我正在尝试将大文件从 Windows Phone 8 设备上传到 SkyDrive。为了避免 OurOfMemory 异常,我必须手动(不使用 SDK)写入文件字节以使用 AllowWriteStreamBuffering = false 请求流
var putUrl = string.Format("https://apis.live.net/v5.0/{0}/files/{1}?suppress_response_codes=true&suppress_redirects=true&overwrite=true&access_token={2}", _folderId, fileName, _session.AccessToken);
var webRequest = (HttpWebRequest) WebRequest.Create(putUrl);
webRequest.ContentType = string.Empty;//Empty is required for PUT method.
webRequest.Method = "PUT";
webRequest.AllowWriteStreamBuffering = false;
webRequest.ContentLength = fileStream.Length;
webRequest.BeginGetRequestStream(iar =>
{
using (var requestStream = webRequest.EndGetRequestStream(iar))
{
var buffer = new byte[1024*100];
var read = 0;
while ((read = fileStream.Read(buffer, 0, buffer.Length)) <= buffer.Length)
{
requestStream.Write(buffer, 0, read); //!!!IT HANGS HERE WITHOUT ANY ERROR!!!
if (read < buffer.Length)
break;
}
}
}, webRequest);
它适用于中小型文件(<50MB),但在 requestStream.Write 上传挂起(以不同的百分比,有时是 30%,有时是 50%)并且不会继续执行大文件(100-500 MB)。
我很感激任何帮助。
谢谢, 阿列克谢·斯特拉赫