【原創】MVC項目中使用JQuery的upladify圖片上傳插件相關問題的解決方案
阿新 • • 發佈:2018-07-05
ack pty let protect 失效 wid min fun adding
一. 關於Uploadify
Uploadify是一個jQuery插件,你可以很容易的為你的網站添加多個文件上傳功能。有兩個不同的版本(HTML5和Flash)允許你靈活選擇為您的網站和回退方法正確實施使其降解優雅。
二. 問題整理
問題1:頁面報插件的圖片找不到
遇到這個問題,很明顯是相關文件中引用的路徑不正確。
找到下邊這個文件,修改相關路徑為正確的路徑。
問題2:頁面報net::ERR_NAME_NOT_RESOLVED
找到下邊這個文件
在插件主文件查找
1 this.settings.button_image_url=SWFUpload.completeURL(this.settings.button_image_url);
將其改為:
1 if(this.settings.button_image_url!=""){ 2 this.settings.button_image_url=SWFUpload.completeURL(this.settings.button_image_url); 3 }
註意:此處要粘貼復制不要使用vs的自帶替換功能,避免將其他代碼錯誤替換。
問題3:服務器傳session
首先在Global.asax配置文件下添加兩個獲取session方法
方法1:
1 protected void Application_BeginRequest(objectsender, EventArgs e) 2 { 3 /* we guess at this point session is not already retrieved by application so we recreate cookie with the session id... */ 4 try 5 { 6 string session_param_name = "ASPSESSID"; 7 string session_cookie_name = "ASP.NET_SessionId"; 8 9 if (HttpContext.Current.Request.Form[session_param_name] != null) 10 { 11 UpdateCookie(session_cookie_name, HttpContext.Current.Request.Form[session_param_name]); 12 } 13 else if (HttpContext.Current.Request.QueryString[session_param_name] != null) 14 { 15 UpdateCookie(session_cookie_name, HttpContext.Current.Request.QueryString[session_param_name]); 16 } 17 } 18 catch 19 { 20 } 21 22 try 23 { 24 string auth_param_name = "AUTHID"; 25 string auth_cookie_name = FormsAuthentication.FormsCookieName; 26 27 if (HttpContext.Current.Request.Form[auth_param_name] != null) 28 { 29 UpdateCookie(auth_cookie_name, HttpContext.Current.Request.Form[auth_param_name]); 30 } 31 else if (HttpContext.Current.Request.QueryString[auth_param_name] != null) 32 { 33 UpdateCookie(auth_cookie_name, HttpContext.Current.Request.QueryString[auth_param_name]); 34 } 35 36 } 37 catch 38 { 39 } 40 }
方法2:
1 private void UpdateCookie(string cookie_name, string cookie_value) 2 { 3 HttpCookie cookie = HttpContext.Current.Request.Cookies.Get(cookie_name); 4 if (null == cookie) 5 { 6 cookie = new HttpCookie(cookie_name); 7 } 8 cookie.Value = cookie_value; 9 HttpContext.Current.Request.Cookies.Set(cookie); 10 }
在使用的界面添加取值操作,要添加在使用插件的上面(這裏使用C#中的 @符號在頁面中獲取,並作為JS代碼中的全局變量)
1 var auth = "@(Request.Cookies[FormsAuthentication.FormsCookieName]==null ? string.Empty : Request.Cookies[FormsAuthentication.FormsCookieName].Value)"; 2 var ASPSESSID = "@Session.SessionID";
然後在插件的formData裏面寫入:
1 ‘folder‘: ‘/Upload‘, ‘ASPSESSID‘: ASPSESSID, ‘AUTHID‘: auth
整個效果如下:
1 <link href="~/Content/uploadify/uploadify.css" rel="stylesheet" /> 2 <script src="~/Content/uploadify/jquery.uploadify.min.js"></script> 3 <script type="text/javascript"> 4 //上傳圖片 5 $(function () { 6 var auth = "@(Request.Cookies[FormsAuthentication.FormsCookieName]==null ? string.Empty : Request.Cookies[FormsAuthentication.FormsCookieName].Value)"; 7 var ASPSESSID = "@Session.SessionID"; 8 $("#btnUpload").uploadify({ 9 buttonText: ‘上傳圖片‘, 10 height: 20, 11 width: 120, 12 swf: ‘/Content/uploadify/uploadify.swf‘, 13 uploader: ‘/Back_Areas/PhotoSlider/Upload‘,//通過後臺的程序把文件上傳到服務器 14 multi: false,//是否允許同時選擇多個文件 15 fileSizeLimit: ‘8MB‘,//文件大小 16 fileTypeExts: ‘*.gif;*.png;*.jpg;*jpeg‘,//可選文件的擴展名 17 formData: { 18 ‘folder‘: ‘/Upload‘, ‘ASPSESSID‘: ASPSESSID, ‘AUTHID‘: auth//測試 19 }, 20 onUploadSuccess: function (file, data, response) { 21 var jsonData = $.parseJSON(data); 22 $.procAjaxMsg(jsonData, function () { 23 $.alertMsg(jsonData.Msg, ‘操作提示‘, function () { 24 $("#uImgUrl").attr("src", jsonData.BackUrl); 25 $("#h_uImgUrl").val(jsonData.BackUrl); 26 }); 27 }, function () { 28 $.alertMsg(jsonData.Msg, ‘操作提示‘, null); 29 }); 30 }, 31 onUploadError: function (file, errorCode, errorMsg, errorString) { 32 $.alertMsg(‘文件 ‘ + file.name + ‘ 上傳失敗: ‘ + errorString, ‘上傳失敗‘, null); 33 }, 34 onSelectError: function (file, errorCode, errorMsg, errorString) { 35 $.alertMsg(‘文件 ‘ + file.name + ‘ 不能被上傳: ‘ + errorString, ‘選擇失效‘, null); 36 } 37 38 }); 39 }); 40 41 </script>
【原創】MVC項目中使用JQuery的upladify圖片上傳插件相關問題的解決方案