Uploading images using jquery uploadify 302 error study for half a day, found that I upload the action in accordance with the session to determine whether the user is logged in, if not logged on to jump to the landing page, so there are 302 jump error. After updating the version of Flash, the value of session cannot be obtained by uploadify. My current solution is to:
In Global.asax.cs:
/// <summary> ///to resolve a situation where uploadif uploads cannot obtain cookies/// </summary> /// <param name= "Sender" ></param> /// <param name= "E" ></param> protected voidApplication_BeginRequest (Objectsender, EventArgs e) { Try{HttpRequest request=HttpContext.Current.Request; foreach(stringKinchrequest. Form.keys) {if(K.indexof ("Cookie_") >=0) appendingcookies (K.remove (0,7), request. FORM[K], request); } } Catch { } } Private voidAppendingcookies (stringCookieName,stringCookievalue, HttpRequest request) {HttpCookie HC=request. Cookies.get (CookieName); if(NULL==HC) HC=NewHttpCookie (cookiename); Hc. Value=Cookievalue; Request. Cookies.set (HC); }
Upload method:
[HttpPost] Publicjsonresult Upload (httppostedfilebase fileData) {stringCookie_loginuserkey = request.form["Cookie_loginuserkey"]; if(Realcookie! =Cookie_loginuserkey) { returnJson ("Upload failed"); } if(FileData! =NULL) { Try { stringCompanyID =ManageProvider.Provider.Current (). CompanyID; Sys_company Model=COMPANYBLL. GetEntity (CompanyID); stringCompanycode =model. Code; stringfolder = DateTime.Now.ToString ("yyyymm"); //save path after file upload stringFilePath = Server.MapPath ("~/uploads/"+ Companycode +"/web/"+ Folder +"/"); 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 2MB"}, Jsonrequestbehavior.allowget); } Else{filedata.saveas (FilePath+savename); returnJson (New{Success =true, FilePath ="/uploads/"+ Companycode +"/web/"+ Folder +"/"+ Savename, Savename =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); } }
In the View:
$ (document). Ready (function () { $(' #file_upload '). Uploadify ({uploader:'/school/sites/upload ', SwF:'/content/scripts/uploadify3.2.1/uploadify.swf ', ButtonText:"Please select Upload image", Height:24, Width:120, ' Fileext ': ' *.jpg;*.gif,*.png ',//the file format allowed for uploading is *.jpg,*.gif,*.png //' filedesc ': ' Web Image Files (. Jpg. Gif. PNG) ',//filter out files except *.jpg,*.gif,*.png //' Queueid ': ' Filequeue ',' SizeLimit ': ' 2048000 ',//the maximum allowable file size is 2M //' auto ': false,//need to submit the request manually' Multi ':false,//allow only one image to be uploaded at a timeFormData: {@foreach (string KinchRequest.Cookies.AllKeys) {@:' [email protected] (k) ': ' @Request. cookies[k]. Value ', } ‘‘: ‘‘ }, ' Onuploadsuccess ':function(file, data, response) {varobj = Jquery.parsejson (data);//converts the returned JSON sequence to an obj object if(obj. Success) {//alert (obj. FilePath);$ (' #PictureUrl '). val (obj. FilePath); $(' #pic '). attr ("src"), obj. FilePath); $(' #pic '). Show (); } Elsealert (obj. Message); } }); });
Using jquery uploadify to upload images in MVC 302 error