1. Note that adding the enctype = "Multipart/form-data" property to the form form will cause the action's parameter HttpPostedFileBase object to receive no files.
2. Note The file size, IIS default upload file size is 4MB, more than this size of files need to modify the configuration file.
3. If "Js/jquery.mobile.min.js" is quoted, add data-ajax= "false" to the form form, otherwise the uploaded file will not be received.
if(!Request.Content.IsMimeMultipartContent ()) { Throw Newhttpresponseexception (Httpstatuscode.unsupportedmediatype); } stringRoot =@"c:/"; varProvider =NewMultipartformdatastreamprovider (root); Try{StringBuilder SB=NewStringBuilder (); varTask =Request.Content.ReadAsMultipartAsync (provider); Task. Wait (); foreach(varFileinchprovider. FileData) {FileInfo FileInfo=NewFileInfo (file. LocalFilename); Sb. Append (string. Format ("uploaded file: {0} ({1} bytes) \ n", Fileinfo.name, fileinfo.length)); } return NewHttpresponsemessage () {Content=Newstringcontent (sb.) ToString ())}; } Catch(System.Exception e) {returnRequest.createerrorresponse (Httpstatuscode.internalservererror, E); }
View Code
WebApi File Upload