Uploading file to server throws out of memory exception
One solution would be to use MultipartFormDataStreamProvider
instead of the MultipartMemoryStreamProvider
to avoid the out of memory exception during the call
Request.Content.ReadAsMultipartAsync(..)
I was facing a similar problem while trying to use a MemoryStreamProvider while reading the MultiPart file contents for a large file (> 100 MB). The work around that worked for me was to use MultipartFormDataStreamProvider
. The file is written to the disk during the ReadAsMultipartAsync call and can be later loaded back in if you need it in memory.
Here is an example taken from:
Sending HTML Form Data in Web API: File Upload and Multipart MIME
string root = HttpContext.Current.Server.MapPath("~/App_Data");
var provider = new MultipartFormDataStreamProvider(root);
try
{
// Read the form data.
await Request.Content.ReadAsMultipartAsync(provider);
// This illustrates how to get the file names.
foreach (MultipartFileData file in provider.FileData)
{
Trace.WriteLine(file.Headers.ContentDisposition.FileName);
Trace.WriteLine("Server file path: " + file.LocalFileName);
}
return Request.CreateResponse(HttpStatusCode.OK);
}
catch(...)