jQuery插件ajaxFileUpload使用實例解析
ajaxFileUpload.js 很多同名的,因為做出來一個很容易。
我用的是這個:https://github.com/carlcarl/AjaxFileUpload
下載地址在這里:http://xiazai.jb51.net/201610/yuanma/ajaxfileupload(jb51.net).rar
AjaxFileUpload.js并不是一個很出名的插件,只是別人寫好的放出來供大家用,原理都是創(chuàng)建隱藏的表單和iframe然后用JS去提交,獲得返回值。
當初做了個異步上傳的功能,選擇它因為它的配置方式比較像jQuery的AJAX,我很喜歡。
評論里面說到的不行。那是因為我們用的不是同一個js。我上github搜AjaxFileUpload出來很多類似js。
ajaxFileUpload是一個異步上傳文件的jQuery插件
傳一個不知道什么版本的上來,以后不用到處找了。
語法:$.ajaxFileUpload([options])
options參數(shù)說明:
1、url 上傳處理程序地址?! ?br />
2,fileElementId 需要上傳的文件域的ID,即<input type="file">的ID。
3,secureuri 是否啟用安全提交,默認為false。
4,dataType 服務(wù)器返回的數(shù)據(jù)類型。可以為xml,script,json,html。如果不填寫,jQuery會自動判斷。
5,success 提交成功后自動執(zhí)行的處理函數(shù),參數(shù)data就是服務(wù)器返回的數(shù)據(jù)。
6,error 提交失敗自動執(zhí)行的處理函數(shù)。
7,data 自定義參數(shù)。這個東西比較有用,當有數(shù)據(jù)是與上傳的圖片相關(guān)的時候,這個東西就要用到了。
8, type 當要提交自定義參數(shù)時,這個參數(shù)要設(shè)置成post
錯誤提示:
1、SyntaxError: missing ; before statement錯誤
如果出現(xiàn)這個錯誤就需要檢查url路徑是否可以訪問
2、SyntaxError: syntax error錯誤
如果出現(xiàn)這個錯誤就需要檢查處理提交操作的服務(wù)器后臺處理程序是否存在語法錯誤
3、SyntaxError: invalid property id錯誤
如果出現(xiàn)這個錯誤就需要檢查文本域?qū)傩訧D是否存在
4、SyntaxError: missing } in XML expression錯誤
如果出現(xiàn)這個錯誤就需要檢查文件name是否一致或不存在
5、其它自定義錯誤
大家可使用變量$error直接打印的方法檢查各參數(shù)是否正確,比起上面這些無效的錯誤提示還是方便很多。
使用方法:
第一步:先引入jQuery與ajaxFileUpload插件。注意先后順序,這個不用說了,所有的插件都是這樣。
<script src="jquery-1.7.1.js" type="text/javascript"></script> <script src="ajaxfileupload.js" type="text/javascript"></script>
第二步:HTML代碼:
<body> <p><input type="file" id="file1" name="file" /></p> <input type="button" value="上傳" /> <p><img id="img1" alt="上傳成功啦" src="" /></p> </body>
第三步:JS代碼
<script src="jquery-1.7.1.js" type="text/javascript"></script> <script src="ajaxfileupload.js" type="text/javascript"></script> <script type="text/javascript"> $(function () { $(":button").click(function () { ajaxFileUpload(); }) }) function ajaxFileUpload() { $.ajaxFileUpload ( { url: '/upload.aspx', //用于文件上傳的服務(wù)器端請求地址 secureuri: false, //是否需要安全協(xié)議,一般設(shè)置為false fileElementId: 'file1', //文件上傳域的ID dataType: 'json', //返回值類型 一般設(shè)置為json success: function (data, status) //服務(wù)器成功響應處理函數(shù) { $("#img1").attr("src", data.imgurl); if (typeof (data.error) != 'undefined') { if (data.error != '') { alert(data.error); } else { alert(data.msg); } } }, error: function (data, status, e)//服務(wù)器響應失敗處理函數(shù) { alert(e); } } ) return false; } </script>
第四步:后臺頁面upload.aspx代碼:
protected void Page_Load(object sender, EventArgs e) { HttpFileCollection files = Request.Files; string msg = string.Empty; string error = string.Empty; string imgurl; if (files.Count > 0) { files[0].SaveAs(Server.MapPath("/") + System.IO.Path.GetFileName(files[0].FileName)); msg = " 成功! 文件大小為:" + files[0].ContentLength; imgurl = "/" + files[0].FileName; string res = "{ error:'" + error + "', msg:'" + msg + "',imgurl:'" + imgurl + "'}"; Response.Write(res); Response.End(); } }
本實例完整代碼下載
來一個MVC版本的實例:
控制器代碼
public class HomeController : Controller { public ActionResult Index() { return View(); } public ActionResult Upload() { HttpFileCollection hfc = System.Web.HttpContext.Current.Request.Files; string imgPath = ""; if (hfc.Count > 0) { imgPath = "/testUpload" + hfc[0].FileName; string PhysicalPath = Server.MapPath(imgPath); hfc[0].SaveAs(PhysicalPath); } return Content(imgPath); } }
前端視圖,HTML與JS代碼,成功上傳后,返回圖片真實地址并綁定到<img>的SRC地址
<html> <head> <script src="/jquery-1.7.1.js" type="text/javascript"></script> <script src="/ajaxfileupload.js" type="text/javascript"></script> <script type="text/javascript"> $(function () { $(":button").click(function () { if ($("#file1").val().length > 0) { ajaxFileUpload(); } else { alert("請選擇圖片"); } }) }) function ajaxFileUpload() { $.ajaxFileUpload ( { url: '/Home/Upload', //用于文件上傳的服務(wù)器端請求地址 secureuri: false, //一般設(shè)置為false fileElementId: 'file1', //文件上傳空間的id屬性 <input type="file" id="file" name="file" /> dataType: 'HTML', //返回值類型 一般設(shè)置為json success: function (data, status) //服務(wù)器成功響應處理函數(shù) { alert(data); $("#img1").attr("src", data); if (typeof (data.error) != 'undefined') { if (data.error != '') { alert(data.error); } else { alert(data.msg); } } }, error: function (data, status, e)//服務(wù)器響應失敗處理函數(shù) { alert(e); } } ) return false; } </script> </head> <body> <p><input type="file" id="file1" name="file" /></p> <input type="button" value="上傳" /> <p><img id="img1" alt="上傳成功啦" src="" /></p> </body> </html>
最后再來一個上傳圖片且附帶參數(shù)的實例:控制器代碼:
public class HomeController : Controller { public ActionResult Index() { return View(); } public ActionResult Upload() { NameValueCollection nvc = System.Web.HttpContext.Current.Request.Form; HttpFileCollection hfc = System.Web.HttpContext.Current.Request.Files; string imgPath = ""; if (hfc.Count > 0) { imgPath = "/testUpload" + hfc[0].FileName; string PhysicalPath = Server.MapPath(imgPath); hfc[0].SaveAs(PhysicalPath); } //注意要寫好后面的第二第三個參數(shù) return Json(new { Id = nvc.Get("Id"), name = nvc.Get("name"), imgPath1 = imgPath },"text/html", JsonRequestBehavior.AllowGet); } }
Index視圖代碼:
<html> <head> <script src="/jquery-1.7.1.js" type="text/javascript"></script> <script src="/ajaxfileupload.js" type="text/javascript"></script> <script type="text/javascript"> $(function () { $(":button").click(function () { if ($("#file1").val().length > 0) { ajaxFileUpload(); } else { alert("請選擇圖片"); } }) }) function ajaxFileUpload() { $.ajaxFileUpload ( { url: '/Home/Upload', //用于文件上傳的服務(wù)器端請求地址 type: 'post', data: { Id: '123', name: 'lunis' }, //此參數(shù)非常嚴謹,寫錯一個引號都不行 secureuri: false, //一般設(shè)置為false fileElementId: 'file1', //文件上傳空間的id屬性 <input type="file" id="file" name="file" /> dataType: 'json', //返回值類型 一般設(shè)置為json success: function (data, status) //服務(wù)器成功響應處理函數(shù) { alert(data); $("#img1").attr("src", data.imgPath1); alert("你請求的Id是" + data.Id + " " + "你請求的名字是:" + data.name); if (typeof (data.error) != 'undefined') { if (data.error != '') { alert(data.error); } else { alert(data.msg); } } }, error: function (data, status, e)//服務(wù)器響應失敗處理函數(shù) { alert(e); } } ) return false; } </script> </head> <body> <p><input type="file" id="file1" name="file" /></p> <input type="button" value="上傳" /> <p><img id="img1" alt="上傳成功啦" src="" /></p> </body> </html>
此實例在顯示出異步上傳圖片的同時并彈出自定義傳輸?shù)膮?shù)。本實例下載地址
2013年1月28日,今天調(diào)試過程中發(fā)現(xiàn)一個問題,就是作為文件域(<input type="file">)必須要有name屬性,如果沒有name屬性,上傳之后服務(wù)器是獲取不到圖片的。如:正確的寫法是<input type="file" id="file1" name="file1" />
2013年1月28日,最經(jīng)典的錯誤終于找到原因所在了。Object function (a,b){return new e.fn.init(a,b,h)} has no method 'handleError',這個是google瀏覽器報的錯誤,非常經(jīng)典, 不知道是我的版本問題還是真正存在的問題。這個問題的根源經(jīng)過N次上傳才找到問題的根本所在。答案是:dataType參數(shù)一定要大寫。如:dataType: 'HTML'。
2016-07-28,評論中的一個錯誤:TypeError: $.ajaxFileUpload is not a function 我們用的不是同一個JS,你下了別的AJAXFileUpload去了。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
- jQuery插件ajaxFileUpload異步上傳文件
- Asp.net MVC中使用JQuery插件ajaxFileUpload上傳文件
- jQuery插件ajaxfileupload.js實現(xiàn)上傳文件
- jQuery插件AjaxFileUpload實現(xiàn)ajax文件上傳
- PHP結(jié)合jQuery插件ajaxFileUpload實現(xiàn)異步上傳文件實例
- jQuery異步上傳文件插件ajaxFileUpload詳細介紹
- JQuery插件ajaxfileupload.js異步上傳文件實例
- 一個簡單的jQuery插件ajaxfileupload.js實現(xiàn)ajax上傳文件例子
- 為jquery的ajaxfileupload增加附加參數(shù)的方法
- jQuery Ajax File Upload實例源碼
相關(guān)文章
jquery獲取子節(jié)點和父節(jié)點的示例代碼
獲取子節(jié)點和父節(jié)點的方法有很多,在本文為大家詳細介紹下jquery中時如何實現(xiàn)的,感興趣的朋友可以參考下2013-09-09easyui-datagrid開發(fā)實踐(總結(jié))
本篇文章主要介紹了easyui-datagrid開發(fā)實踐(總結(jié)),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-08-08給artDialog 5.02 增加ajax get功能詳細介紹
本文將詳細介紹給artDialog 5.02 增加ajax get功能的方法,按興趣的朋友可以參考2012-11-11jQuery使用bind函數(shù)實現(xiàn)綁定多個事件的方法
這篇文章主要介紹了jQuery使用bind函數(shù)實現(xiàn)綁定多個事件的方法,結(jié)合簡單實例形式分析了jQuery使用bind函數(shù)進行多個事件綁定的操作技巧與注意事項,需要的朋友可以參考下2017-10-10