I am trying to upload files using similar approach HttpClient: How to upload multiple files at once in windows phone.
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;
}
This works fine while uploading small files. If I tried to upload larger file(say > 50mb) in a low end device(512Mb memory), it throws System.OutOfMemoryException. I used the diagnostic tools to monitor the memory consumption and noticed that memory grows exponentially during PostAsync call. Seems like it is copying the entire content to the memory. Right now we don't have chunking support in the api.
What is the best strategy to upload large file using HttpClient in a low memory windows phone device?