Basically I'm trying to upload an image along with an enum using Web API 2.
基本上我正在尝试使用Web API 2上传图像和枚举。
Here's the controller signature:
这是控制器签名:
[HttpPost]
public UploadResponseVm Upload([FromBody]ResImageType type)
{
The thing is, whenever I try to post a multipart form (with a file and a type) I get a 415 error:
问题是,每当我尝试发布多部分表单(带有文件和类型)时,我都会收到415错误:
{"Message":"The request entity's media type 'multipart/form-data' is not supported for this resource.","ExceptionMessage":"No MediaTypeFormatter is available to read an object of type 'ResImageType' from content with media type 'multipart/form-data'.","ExceptionType":"System.Net.Http.UnsupportedMediaTypeException","StackTrace":" at System.Net.Http.HttpContentExtensions.ReadAsAsync[T](HttpContent content, Type type, IEnumerable
1 formatters, IFormatterLogger formatterLogger, CancellationToken cancellationToken)\r\n at System.Web.Http.ModelBinding.FormatterParameterBinding.ReadContentAsync(HttpRequestMessage request, Type type, IEnumerable
1 formatters, IFormatterLogger formatterLogger, CancellationToken cancellationToken)"}{“Message”:“此资源不支持请求实体的媒体类型'multipart / form-data'。”,“ExceptionMessage”:“没有MediaTypeFormatter可用于从具有媒体类型的内容中读取'ResImageType'类型的对象'multipart / form-data'。“,”ExceptionType“:”System.Net.Http.UnsupportedMediaTypeException“,”StackTrace“:”at System.Net.Http.HttpContentExtensions.ReadAsAsync [T](HttpContent内容,类型类型,IEnumerable1 formatters,IFormatterLogger formatterLogger,CancellationToken cancellationToken)\ r \ n在System.Web.Http.ModelBinding.FormatterParameterBinding.ReadContentAsync(HttpRequestMessage请求,类型类型,IEnumerable1格式化程序,IFormatterLogger formatterLogger,CancellationToken cancellationToken)“}
I have even added the following to my startup.cs class:
我甚至在startup.cs类中添加了以下内容:
config.Formatters.Insert(0, new System.Net.Http.Formatting.JsonMediaTypeFormatter());
How can I upload a model along with a file using a web api controller?
如何使用web api控制器上传模型和文件?
1 个解决方案
#1
0
There is no formatter that could handle/relate to your ResImageType
object. I have once solved a similar problem without a formatter by using a parameter-less method and have processed the data inside the method. For example:
没有可以处理/与您的ResImageType对象相关的格式化程序。我曾经使用无参数方法解决了没有格式化程序的类似问题,并处理了方法中的数据。例如:
public async Task<HttpResponseMessage> PostFormData()
{
if (!Request.Content.IsMimeMultipartContent("form-data"))
{
return Request.CreateResponse(HttpStatusCode.BadRequest);
}
string upDir= "PathOfDirectory";
MultipartFormDataStreamProvider streamProvider = new MultipartFormDataStreamProvider(upDir);
MultipartFileStreamProvider multipartFileStreamProvider = await Request.Content.ReadAsMultipartAsync(streamProvider);
// Loop through files.
foreach (MultipartFileData file in streamProvider.FileData)
{
// Save filepaths to DB or do something else
}
return Request.CreateResponse(HttpStatusCode.OK);
}
Similar solution from MS Docs
来自MS Docs的类似解决方案
Another possibility is to create a DTO-like class that is used to transfer the object and use a formatter, for example MultipartDataMediaFormatter seems legit (haven't tried).
另一种可能性是创建一个类似DTO的类,用于传输对象并使用格式化程序,例如MultipartDataMediaFormatter似乎合法(尚未尝试过)。
#1
0
There is no formatter that could handle/relate to your ResImageType
object. I have once solved a similar problem without a formatter by using a parameter-less method and have processed the data inside the method. For example:
没有可以处理/与您的ResImageType对象相关的格式化程序。我曾经使用无参数方法解决了没有格式化程序的类似问题,并处理了方法中的数据。例如:
public async Task<HttpResponseMessage> PostFormData()
{
if (!Request.Content.IsMimeMultipartContent("form-data"))
{
return Request.CreateResponse(HttpStatusCode.BadRequest);
}
string upDir= "PathOfDirectory";
MultipartFormDataStreamProvider streamProvider = new MultipartFormDataStreamProvider(upDir);
MultipartFileStreamProvider multipartFileStreamProvider = await Request.Content.ReadAsMultipartAsync(streamProvider);
// Loop through files.
foreach (MultipartFileData file in streamProvider.FileData)
{
// Save filepaths to DB or do something else
}
return Request.CreateResponse(HttpStatusCode.OK);
}
Similar solution from MS Docs
来自MS Docs的类似解决方案
Another possibility is to create a DTO-like class that is used to transfer the object and use a formatter, for example MultipartDataMediaFormatter seems legit (haven't tried).
另一种可能性是创建一个类似DTO的类,用于传输对象并使用格式化程序,例如MultipartDataMediaFormatter似乎合法(尚未尝试过)。