C
C#2y ago
Utsuhoagie

❔ Why is my ASP.NET Core Web API not responding at all to a `multipart/form-data` request?

Specifically my client is Expo Go (React Native) on a physical Android, and the request does work in Postman. On my server:
C#
Models/FileUploadRequest.cs
public class FileUploadRequest
{
public IFormFile File1 { get; set; } = default!;
}

Controllers/FilesController.cs
[HttpPost("Upload")]
public async Task<IActionResult> Upload([FromForm] FileUploadRequest req)
{
var file1 = req.File1;
if (file1.Length > 0)
{
var safeFileName = Path.GetRandomFileName();
var safeFilePathName = Path.Combine(DANGEROUS_FILE_PATH, safeFileName);
var fileStream = System.IO.File.Create(safeFilePathName);
await file1.CopyToAsync(fileStream);
}
return Ok(new
{
Message = "File is uploaded",
File = file1
});
}
C#
Models/FileUploadRequest.cs
public class FileUploadRequest
{
public IFormFile File1 { get; set; } = default!;
}

Controllers/FilesController.cs
[HttpPost("Upload")]
public async Task<IActionResult> Upload([FromForm] FileUploadRequest req)
{
var file1 = req.File1;
if (file1.Length > 0)
{
var safeFileName = Path.GetRandomFileName();
var safeFilePathName = Path.Combine(DANGEROUS_FILE_PATH, safeFileName);
var fileStream = System.IO.File.Create(safeFilePathName);
await file1.CopyToAsync(fileStream);
}
return Ok(new
{
Message = "File is uploaded",
File = file1
});
}
On my client, I'm creating a request like this:
// Get photo from URI
const res = await fetch(photo.uri);
const img = await res.blob();

const formData = new FormData();
formData.append('File1', img);
const uploadRes = await API.post('Files/Upload', formData, {
headers: { 'Content-Type': 'multipart/form-data' },
});
// Get photo from URI
const res = await fetch(photo.uri);
const img = await res.blob();

const formData = new FormData();
formData.append('File1', img);
const uploadRes = await API.post('Files/Upload', formData, {
headers: { 'Content-Type': 'multipart/form-data' },
});
But whenever I try to send this request, the server does not respond at all. However, if I remove the headers (default Content-Type: application/json) then it does respond with 400, along with these logs:
dbug: Microsoft.AspNetCore.Mvc.ModelBinding.Binders.FormFileModelBinder[13]
Attempting to bind property 'Capstone.Features.File.FileUploadRequest.File1' of type 'Microsoft.AspNetCore.Http.IFormFile' using the name 'Fi
le1' in request data ...
dbug: Microsoft.AspNetCore.Mvc.ModelBinding.Binders.FormFileModelBinder[19]
Could not bind to model with name 'File1' and type 'Microsoft.AspNetCore.Http.IFormFile' as the request did not have a content type of either
'application/x-www-form-urlencoded' or 'multipart/form-data'.
dbug: Microsoft.AspNetCore.Mvc.ModelBinding.Binders.FormFileModelBinder[13]
Attempting to bind property 'Capstone.Features.File.FileUploadRequest.File1' of type 'Microsoft.AspNetCore.Http.IFormFile' using the name 'Fi
le1' in request data ...
dbug: Microsoft.AspNetCore.Mvc.ModelBinding.Binders.FormFileModelBinder[19]
Could not bind to model with name 'File1' and type 'Microsoft.AspNetCore.Http.IFormFile' as the request did not have a content type of either
'application/x-www-form-urlencoded' or 'multipart/form-data'.
3 Replies
trustnooneelse
well in my case i needed to add to the post method following attribute:
[Consumes("multipart/form-data")]
[Consumes("multipart/form-data")]
Utsuhoagie
UtsuhoagieOP2y ago
Still doesn't work. The weird thing is that it does work in Postman, but not in my Expo Go (React Native on physical Android) app Now that I think about it, since it does work in Postman with a similar request (uploading a file from my PC instead of getting the file from a URI on the Android itself), it's probably a problem on the mobile app's end and not my server, so here's probably not where to ask it
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server