Hi @Stesvis ,
I was able to get rid of the original error by adding this to WebApiConfig.cs:
This only makes ASP.NET route multipart/form-data requests to your controllers. It won't be able to deserialize the form values and bind them to your method's parameters.
You can upload files to the API directly through the HTML form.
Sending HTML Form Data in ASP.NET Web API: File Upload and Multipart MIME
<form name="form1" method="post" enctype="multipart/form-data" action="/api/test">
<div>
<label for="Data">Data</label>
<input name="Data" type="text" />
</div>
<div>
<label for="File"> File</label>
<input name="File" type="file" />
</div>
<div>
<input type="submit" value="Submit" />
</div>
</form>
public class TestController : ApiController
{
[HttpPost]
public async Task<HttpResponseMessage> PostFormData()
{
// Check if the request contains multipart/form-data.
if (!Request.Content.IsMimeMultipartContent())
{
throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
}
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);//get FileName
Trace.WriteLine("Server file path: " + file.LocalFileName);//get File Path
}
return Request.CreateResponse(HttpStatusCode.OK, "pass upload file successed!");
}
catch (System.Exception e)
{
return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e);
}
}
}
Best regards,
Lan Huang
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.