【发布时间】:2015-01-19 01:25:03
【问题描述】:
我正在尝试使用类似的方法上传文件HttpClient: How to upload multiple files at once 在 Windows 手机中。
using (var content = new MultipartFormDataContent())
{
content.Add(CreateFileContent(imageStream, "image.jpg", "image/jpeg"));
content.Add(CreateFileContent(signatureStream, "image.jpg.sig", "application/octet-stream"));
var response = await httpClient.PostAsync(_profileImageUploadUri, content);
response.EnsureSuccessStatusCode();
}
private StreamContent CreateFileContent(Stream stream, string fileName, string contentType)
{
var fileContent = new StreamContent(stream);
fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
{
Name = "\"files\"",
FileName = "\"" + fileName + "\""
}; // the extra quotes are key here
fileContent.Headers.ContentType = new MediaTypeHeaderValue(contentType);
return fileContent;
}
这在上传小文件时可以正常工作。如果我尝试在低端设备(512Mb 内存)中上传更大的文件(比如 > 50mb), 它抛出 System.OutOfMemoryException。我使用诊断工具来监控内存消耗并注意到内存 在 PostAsync 调用期间呈指数增长。似乎是将整个内容复制到内存中。现在我们在 api。
在低内存 Windows 手机设备中使用 HttpClient 上传大文件的最佳策略是什么?
【问题讨论】:
-
这里有一个解决方案:stackoverflow.com/questions/26223902/…
-
Windows.Networking.BackgroundTransfer.BackgroundUploader怎么样?看这里:stackoverflow.com/a/27430331/27211 -
所有用于下载和上传的 API 调用都在 SDK 中实现(支持 .net 4.5.1、Windows Phone 8.1 和 Windows Store 8.1 的便携式类库)。据我所知,PCL 中不提供对 Windows.Networking.BackgroundTransfer.BackgroundUploader 的支持,如果我错了,请纠正。我必须使用 HttpClient 来实现它。
标签: c# .net windows-phone-8.1 httpclient