How to setup a webapi controller for multipart/form-data
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I am trying to figure out how to get this done. I was not getting any useful error messages with my code so I used something else to generate something. I have attached that code after the error message. I have found a tutorial on it but I do not know how to implement it with what I have. This is what i currently have
public async Task
error
Object {message: "The request entity's media type 'multipart/form-data' is not supported for this resource.", exceptionMessage: "No MediaTypeFormatter is available to read an obje…om content with media type 'multipart/form-data'.", exceptionType: "System.Net.Http.UnsupportedMediaTypeException", stackTrace: " at System.Net.Http.HttpContentExtensions.ReadAs…atterLogger, CancellationToken cancellationToken)"}exceptionMessage: "No MediaTypeFormatter is available to read an object of type 'HttpPostedFileBase' from content with media type 'multipart/form-data'."exceptionType: "System.Net.Http.UnsupportedMediaTypeException"message: "The request entity's media type 'multipart/form-data' is not supported for this resource."stackTrace: " at System.Net.Http.HttpContentExtensions.ReadAsAsync[T](HttpContent content, Type type, IEnumerable1 formatters, IFormatterLogger formatterLogger, CancellationToken cancellationToken)
Code used to generate error message
[HttpPost] public string UploadFile(HttpPostedFileBase file) { if (file.ContentLength > 0) { var fileName = Path.GetFileName(file.FileName); var path = Path.Combine(HttpContext.Current.Server.MapPath("~/uploads"), fileName); file.SaveAs(path); } return "/uploads/" + file.FileName; }
class
public class File { public int FileId { get; set; } public string FileType { get; set; } public string FileDate { get; set; } public byte[] FilePdf { get; set; } public string FileLocation { get; set; } public string FilePlant { get; set; } public string FileTerm { get; set; } public DateTime? FileUploadDate { get; set; } public string FileUploadedBy { get; set; } public string CompanyName { get; set; } public virtual ApplicationUser User { get; set; } }
回答1:
I normally use the HttpPostedFileBase parameter only in Mvc Controllers. When dealing with ApiControllers try checking the HttpContext.Current.Request.Files property for incoming files instead:
[HttpPost] public string UploadFile() { var file = HttpContext.Current.Request.Files.Count > 0 ? HttpContext.Current.Request.Files[0] : null; if (file != null && file.ContentLength > 0) { var fileName = Path.GetFileName(file.FileName); var path = Path.Combine( HttpContext.Current.Server.MapPath("~/uploads"), fileName ); file.SaveAs(path); } return file != null ? "/uploads/" + file.FileName : null; }
回答2:
You can use something like this
[HttpPost] public async Task AddFile() { if (!Request.Content.IsMimeMultipartContent()) { this.Request.CreateResponse(HttpStatusCode.UnsupportedMediaType); } string root = HttpContext.Current.Server.MapPath("~/temp/uploads"); var provider = new MultipartFormDataStreamProvider(root); var result = await Request.Content.ReadAsMultipartAsync(provider); foreach (var key in provider.FormData.AllKeys) { foreach (var val in provider.FormData.GetValues(key)) { if (key == "companyName") { var companyName = val; } } } // On upload, files are given a generic name like "BodyPart_26d6abe1-3ae1-416a-9429-b35f15e6e5d5" // so this is how you can get the original file name var originalFileName = GetDeserializedFileName(result.FileData.First()); var uploadedFileInfo = new FileInfo(result.FileData.First().LocalFileName); string path = result.FileData.First().LocalFileName; //Do whatever you want to do with your file here return this.Request.CreateResponse(HttpStatusCode.OK, originalFileName ); } private string GetDeserializedFileName(MultipartFileData fileData) { var fileName = GetFileName(fileData); return JsonConvert.DeserializeObject(fileName).ToString(); } public string GetFileName(MultipartFileData fileData) { return fileData.Headers.ContentDisposition.FileName; }
回答3:
This is what solved my problem Add the following line to WebApiConfig.cs
You're getting HTTP 415 "The request entity's media type 'multipart/form-data' is not supported for this resource." because you haven't mention the correct content type in your request.