How to do Multipart-form-data file upload in asp.net core web api? Is it possible to POST both JSON and the image at the same time in a single POST?
Update- .net core 2.0 +
With .net core you can leverage the new IFormFile
interface to upload both the image and properties in the same post. For example:
[HttpPost("content/upload-image")]
public async Task<IActionResult> UploadImage(MyFile upload)
The MyFile class can look like:
public class MyFile
{
public string userId { get; set; }
public IFormFile File { get; set; }
// Other properties
}
You can access the properties
and the file
as follows:
var file = upload.File // This is the IFormFile file
var param = upload.userId // param
To persist/save the file to disk you can do the following:
using (var stream = new FileStream(path, FileMode.Create))
{
await file.File.CopyToAsync(stream);
}
.NET Framework
Yes it is. Depending on the client Framework you're using, you can configure your Web API for Content Type-Multipart, then do something like:
[HttpPost]
[Route("content/upload-image")]
public async Task<HttpResponseMessage> Post()
{
if (!Request.Content.IsMimeMultipartContent())
{
throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
}
// enter code here
}
Define and setup the directory where your image will be saved.
var root = HttpContext.Current.Server.MapPath("~/Content/Images/");
if (!Directory.Exists(root))
{
Directory.CreateDirectory(root);
}
Setup the StreamProvider and attempt to get the model data, which is the JSON you mentioned.
var streamProvider = new MultipartFormDataStreamProvider(root);
var result =
await Request.Content.ReadAsMultipartAsync(streamProvider);
if (result.FormData["model"] == null)
{
throw new HttpResponseException(HttpStatusCode.BadRequest);
}
Now access the files in the request.
try
{
// Deserialize model data to your own DTO
var model = result.FormData["model"];
var formDto = JsonConvert
.DeserializeObject<MyDto>(model, new IsoDateTimeConverter());
var files = result.FileData.ToList();
if (files != null)
{
foreach (var file in files)
{
// Do anything with the file(s)
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With