In the View view:
<link href= "/scripts/uploadify-v3.2.1/uploadify.css" rel= "stylesheet" type= "Text/css"/><script src= "/ Scripts/jquery-1.7.1.min.js "type=" Text/javascript "></script><script src="/Scripts/uploadify-v3.2.1/ Jquery.uploadify.js "type=" Text/javascript "></script><script src="/scripts/uploadify-v3.2.1/ Swfobject.js "></script><script type=" Text/javascript "> $ (document). Ready (function () {$ (' #file_u Pload '). Uploadify ({' ButtonText ': ' Select Upload file ', ' Queueid ': ' Filequeue ',//specify where the upload progress bar shows ' SWF ': ' @Url. Content ("~/scripts/uploadify-v3.2.1/uploadify.swf?ver= ' +math.random () + '") ', ' uploader ': '/home/upload ', ' removecompleted ': true, ' checkexisting ': true, ' filetypedesc ': ' uploaded file Type ', ' fil Etypeexts ': ' *.jpg;*.png;*.gif ', ' filesizelimit ': ' 1024kb ', ' auto ': false, ' multi ': false , ' Queuesizelimit ': 1,//number of uploaded files in a queueLimit ' uploadlimit ': 1,//maximum number of uploads allowed ' height ': +, ' width ': 80, ' Onuploadsuccess ': function (file, data, response) {var obj = Jquery.parsejson (data);//Turn the returned JSON sequence To the Obj object if (obj. Success) {$ (' #input '). val (obj. FilePath); $ (' #upsucc '). Text (' Upload successful! ‘); } else alert (obj. Message); } }); });
<tr> <th> @Html. Labelfor (Model=>model. URL) </th> <td> <input type="file" class="File_upload"Id="File_upload"/> <div id="Filequeue"></div> <a href="javascript:$ (' #file_upload '). Uploadify (' upload ');"> Upload </a><span id="UPSUCC"style="color:red"></span>@Html. textboxfor (M= M.url,New{id="input", @style ="Display:none"}) </> </tr>
Controller in
[HttpPost] Publicjsonresult Upload (httppostedfilebase fileData) {if(FileData! =NULL) { Try { //save path after file upload stringFilePath = Server.MapPath ("~/uploads/"); if(!directory.exists (FilePath)) {directory.createdirectory (FilePath); } stringFileName = Path.getfilename (filedata.filename);//Original file name stringFileExtension = Path.getextension (fileName);//file name extension stringSavename = Guid.NewGuid (). ToString () + fileextension;//Save file name intFileSize = filedata.contentlength/1024x768; if(FileSize >1024x768|| FileSize <=2|| (FileExtension! =". jpg"&& FileExtension! =". PNG"&& FileExtension! =". gif")) { returnJson (New{Success =false, Message ="upload failed! \ r Please upload jpg/png format picture, file size not more than 1MB"}, Jsonrequestbehavior.allowget); } Else{filedata.saveas (FilePath+savename); returnJson (New{Success =true, FilePath ="/uploads/"+savename}); } } Catch(Exception ex) {returnJson (New{Success =false, Message =Ex. Message}, Jsonrequestbehavior.allowget); } } Else { returnJson (New{Success =false, Message ="Please select a file to upload! "}, Jsonrequestbehavior.allowget); } }
HTTP 302 error encountered in background app This is because the plugin is not in the process of the session so public Jsonresult Upload (HttpPostedFileBase fileData) It's best to put it in a controller that doesn't have access rights!