Recently, jQuery. Uploadify and Web API are used in combination for upload. If you encounter any problems, you can also solve them. record them as follows:
Environment: jQuery 1.10.2, Uploadify 3.2.1, SWFObject 2.2, Web API 2
Front-end code (JavaScript ):
$ ("# File-upload" "auto": "multi": "buttonText": "select a file" "swf": "/upload.swf" "uploader ": "/api/up" "uploadLimit": 1 "removeCompleted": "onSelect": "# btn-upload "). prop ("disabled", "onCancel": "# btn-upload "). prop ("disabled", "onUploadStart": "Start upload:" + "onUploadSuccess": "Upload [" + file. name + "] successful" "onUploadError": "Upload [" + file. name + "] failed"
Server code (C #)
The ApiController that receives the file:
System.Web.Http;
Task<> (!Request.Content.IsMimeMultipartContent( root = HttpContext.Current.Server.MapPath( provider = body = result = body.FileData[
Create a CustomMultipartFormDataStreamProvider class inherited from MultipartFormDataStreamProvider to restore the uploaded file name:
CustomMultipartFormDataStreamProvider( sb = StringBuilder((headers.ContentDisposition.FileName ?? DateTime.Now.Ticks.ToString()).Replace(, ).Trim().Replace( , => sb.Replace(invalidChar,
After the above Code is complete, it can run smoothly. After testing, the file has been successfully uploaded to the server. However, the upload result is always displayed as failed and an error is reported: error writing MIME multipart body part to output stream.
This is strange. After half a day on Google, I found related instructions. It turned out to be a Bug in Web API 2 Framework, which is related to the line break of Flash. On the one hand, wait for the MS to be repaired (the MS is already fixed and is expected to be fixed in Web API 2.1). In addition, you can solve the problem in other ways. If not, record the problem first, and then complete this article.
The following are some discussions and explanations related to MS. All of them are in English. If you are interested, you can check them out.
Foreign discussion (English)
Description of MS Development Team (English)
Another description of the MS Development Team)