WebAPI File Uploading - Without writing files to disk
Solved:
Use the existing simple MultipartMemoryStreamProvider
. No custom classes or providers required. This differers from the duplicate question which solved the solution by writing a custom provider.
Then use it in a WebAPI handler as so:
public async Task<IHttpActionResult> UploadFile()
{
if (!Request.Content.IsMimeMultipartContent())
{
return StatusCode(HttpStatusCode.UnsupportedMediaType);
}
var filesReadToProvider = await Request.Content.ReadAsMultipartAsync();
foreach (var stream in filesReadToProvider.Contents)
{
var fileBytes = await stream.ReadAsByteArrayAsync();
}
return StatusCode(HttpStatusCode.OK);
}
Related videos on Youtube
simbolo
Everything From CRM, CMS, eCommerce and business applications from desktop, web and mobile. Strictly web applications only. Special interest in security and privacy.
Updated on June 24, 2020Comments
-
simbolo almost 4 years
All the documentation / tutorials / questions about processing a file uploaded using FormData to a ASP.NET WebAPI handler use
MultipartFormDataStreamProvider
to process the multipart stream to split it into the relevant form fields and files.var root = HttpContext.Current.Server.MapPath("~/App_Data"); var provider = new MultipartFormDataStreamProvider(root); await Request.Content.ReadAsMultipartAsync(provider); foreach (MultipartFileData file in provider.FileData) { // File }
However, the files are automatically written to a directory during processsing.
It seems a lot of hassle when I could just use
HttpContext.Current.Request.Files[0].InputStream
to access a given file stream directly in memory.How can WebAPI just access the file stream directly without the IO overhead of using
MultipartFormDataStreamProvider
?Official tutorial: http://www.asp.net/web-api/overview/advanced/sending-html-form-data,-part-2
-
Jon Susiak over 9 yearsI think This Question should answer your needs.
-
simbolo over 9 yearsThanks @JonSusiak - I wish I found that a few hours ago, I ended up doing the exact same thing by examining the source code or
MultipartFormDataStreamProvider
and using theMultipartStreamProvider
provider to give me the file upload in aMemoryStream
rather than writing it to disk. Was just getting round to posting the answer. -
Merenzo over 7 years@simbolo - why didn't you use
HttpContext.Current.Request.Files[0].InputStream
? Could you explain the disadvantage? -
xdiegom about 6 yearsMicrosoft has a really good example for how to upload files: docs.microsoft.com/en-us/aspnet/web-api/overview/advanced/…
-
-
guiomie almost 9 yearsWhere can I find IHttpActionResult ?
-
simbolo almost 9 years@guiomie simple google: msdn.microsoft.com/en-us/library/…
-
mbx-mbx over 6 yearsCould this code please be updated so that it compiles? Not all code paths return a value. Thanks.
-
George Harnwell over 6 years@Magrangs - you simply need to return a Http Result such as return Ok();
-
Henrik Høyer over 6 yearsend the method with: return Request.CreateResponse(HttpStatusCode.OK);
-
Unbreakable over 5 yearsCan I simply send even big files to this function, or I need to define some
boundary
and other fancy stuff in the client side which sends the file? -
A X over 4 yearsThis doesn't work though - it gets screwed up from an encoding perspective
-
David over 4 yearsWith asp.net 4.7 I am still getting exception Exception. Exception type full name: System.NullReferenceException.] - [System.NullReferenceException: Object reference not set to an instance of an object
-
Brett JB over 4 yearsHi @simbolo, I am also wanting to do this and have managed to extract the file data using your approach. I also have additional form data as well though. Do you have any ideas how I can get to that? The filestoreadprovider is a collection of HttpContent objects, which I have managed to make into a list and walk through, but I cant seem to get at the data. Any Ideas? (Before I throw this out as seperate question..) Thanks...
-
Brent Woodle over 4 yearsThe
MultipartMemoryStreamProvider
doesn't have an API for accessing form data that is included in the request likeMultipartFormDataStreamProvider.FormData
does.