使用jquery uploadify上传图片报302错误研究了半天,发现我上传的action中有根据session判断用户是否登录,如果没有登录就跳到登陆页,所以就出现了302跳转错误。原来更新了flash的版本以后,通过uploadify获取不到session的值。我目前的解决办法是:
Global.asax.cs中:
////// 解决uploadif上传无法获取Cookie的情况 /// /// /// protected void Application_BeginRequest(object sender, EventArgs e) { try { HttpRequest request = HttpContext.Current.Request; foreach (string k in request.Form.Keys) { if (k.IndexOf("cookie_") >= 0) AppendingCookies(k.Remove(0, 7), request.Form[k], request); } } catch { } } private void AppendingCookies(string cookieName, string cookieValue, HttpRequest request) { HttpCookie hc = request.Cookies.Get(cookieName); if (null == hc) hc = new HttpCookie(cookieName); hc.Value = cookieValue; request.Cookies.Set(hc); }
上传方法:
[HttpPost] public JsonResult Upload(HttpPostedFileBase fileData) { string cookie_LoginUserKey = Request.Form["cookie_LoginUserKey"]; if (realCookie != cookie_LoginUserKey) { return Json("上传失败"); } if (fileData != null) { try { string CompanyId = ManageProvider.Provider.Current().CompanyId; Sys_Company model = companybll.GetEntity(CompanyId); string CompanyCode = model.Code; string folder = DateTime.Now.ToString("yyyyMM"); // 文件上传后的保存路径 string filePath = Server.MapPath("~/Uploads/" + CompanyCode + "/Web/" + folder + "/"); if (!Directory.Exists(filePath)) { Directory.CreateDirectory(filePath); } string fileName = Path.GetFileName(fileData.FileName);// 原始文件名称 string fileExtension = Path.GetExtension(fileName); // 文件扩展名 string saveName = Guid.NewGuid().ToString() + fileExtension; // 保存文件名称 int filesize = fileData.ContentLength / 1024; if (filesize > 1024 || filesize <= 2 || (fileExtension != ".jpg" && fileExtension != ".png" && fileExtension != ".gif")) { return Json(new { Success = false, Message = "上传失败!\r请上传jpg/png格式图片,文件大小不要超过2MB" }, JsonRequestBehavior.AllowGet); } else { fileData.SaveAs(filePath + saveName); return Json(new { Success = true, FilePath = "/Uploads/" + CompanyCode + "/Web/" + folder +"/"+ saveName, SaveName = saveName }); } } catch (Exception ex) { return Json(new { Success = false, Message = ex.Message }, JsonRequestBehavior.AllowGet); } } else { return Json(new { Success = false, Message = "请选择要上传的文件!" }, JsonRequestBehavior.AllowGet); } }
视图中:
$(document).ready(function () { $('#file_upload').uploadify({ uploader: '/School/Sites/Upload', swf: '/Content/Scripts/uploadify3.2.1/uploadify.swf', buttonText: "请选择上传图片", height: 24, width: 120, 'fileExt': '*.jpg;*.gif,*.png', //允许上传的文件格式为*.jpg,*.gif,*.png //'fileDesc': 'Web Image Files(.JPG,.GIF,.PNG)', //过滤掉除了*.jpg,*.gif,*.png的文件 //'queueID': 'fileQueue', 'sizeLimit': '2048000', //最大允许的文件大小为2M //'auto': false, //需要手动的提交申请 'multi': false, //一次只允许上传一张图片 formData: { @foreach(string k in Request.Cookies.AllKeys) { @:'cookie_@(k)' : '@Request.Cookies[k].Value', } '': '' }, 'onUploadSuccess': function (file, data, response) { var obj = jQuery.parseJSON(data); //把返回的Json序列转化为obj对象 if (obj.Success) { //alert(obj.FilePath); $('#PictureUrl').val(obj.FilePath); $('#pic').attr("src", obj.FilePath); $('#pic').show(); } else alert(obj.Message); } }); });