国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > AJAX > 正文

jQuery異步上傳文件插件ajaxFileUpload詳細介紹

2024-09-01 08:33:02
字體:
來源:轉載
供稿:網友
這篇文章主要介紹了jQuery異步上傳文件插件ajaxFileUpload詳細介紹,本文首先講解了ajaxFileUpload的參數、錯誤提示等知識,然后給出了簡單使用實例和ASP.NET MVC模式下的使用實例,需要的朋友可以參考下
 

一、ajaxFileUpload是一個異步上傳文件的jQuery插件。

傳一個不知道什么版本的上來,以后不用到處找了。

語法:$.ajaxFileUpload([options])

options參數說明:

1、url            上傳處理程序地址。  
2,fileElementId       需要上傳的文件域的ID,即<input type="file">的ID。
3,secureuri        是否啟用安全提交,默認為false。 
4,dataType        服務器返回的數據類型。可以為xml,script,json,html。如果不填寫,jQuery會自動判斷。
5,success        提交成功后自動執行的處理函數,參數data就是服務器返回的數據。
6,error          提交失敗自動執行的處理函數。
7,data           自定義參數。這個東西比較有用,當有數據是與上傳的圖片相關的時候,這個東西就要用到了。
8, type            當要提交自定義參數時,這個參數要設置成post

錯誤提示:

1,SyntaxError: missing ; before statement錯誤
  如果出現這個錯誤就需要檢查url路徑是否可以訪問
2,SyntaxError: syntax error錯誤
  如果出現這個錯誤就需要檢查處理提交操作的服務器后臺處理程序是否存在語法錯誤
3,SyntaxError: invalid property id錯誤
  如果出現這個錯誤就需要檢查文本域屬性ID是否存在
4,SyntaxError: missing } in XML expression錯誤
  如果出現這個錯誤就需要檢查文件name是否一致或不存在
5,其它自定義錯誤
  大家可使用變量$error直接打印的方法檢查各參數是否正確,比起上面這些無效的錯誤提示還是方便很多。

使用方法:

第一步:先引入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" style="border-left-color: rgb(0, 153, 204); border-left-width: 1px; border-left-style: solid; padding: 0px 3px; margin: 3px auto 0px; width: 640px; background-color: rgb(242, 246, 251); clear: both; border-top-color: rgb(0, 153, 204); border-top-width: 1px; border-top-style: solid; border-right-color: rgb(0, 153, 204); border-right-width: 1px; border-right-style: solid;"> 復制代碼代碼如下:

<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', //用于文件上傳的服務器端請求地址
                    secureuri: false, //是否需要安全協議,一般設置為false
                    fileElementId: 'file1', //文件上傳域的ID
                    dataType: 'json', //返回值類型 一般設置為json
                    success: function (data, status)  //服務器成功響應處理函數
                    {
                        $("#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)//服務器響應失敗處理函數
                    {
                        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', //用于文件上傳的服務器端請求地址
                    secureuri: false, //一般設置為false
                    fileElementId: 'file1', //文件上傳空間的id屬性  <input type="file" id="file" name="file" />
                    dataType: 'HTML', //返回值類型 一般設置為json
                    success: function (data, status)  //服務器成功響應處理函數
                    {
                        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)//服務器響應失敗處理函數
                    {
                        alert(e);
                    }
                }
            )
            return false;
        }
    </script>
</head>
<body>
    <p><input type="file" id="file1" name="file" /></p>
    <input type="button" value="上傳" />
    <p><img id="img1" style="border-left-color: rgb(0, 153, 204); border-left-width: 1px; border-left-style: solid; padding: 0px 3px; margin: 3px auto 0px; width: 640px; background-color: rgb(242, 246, 251); clear: both; border-top-color: rgb(0, 153, 204); border-top-width: 1px; border-top-style: solid; border-right-color: rgb(0, 153, 204); border-right-width: 1px; border-right-style: solid;"> 復制代碼代碼如下:

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);
            }
            //注意要寫好后面的第二第三個參數
            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', //用于文件上傳的服務器端請求地址
                    type: 'post',
                    data: { Id: '123', name: 'lunis' }, //此參數非常嚴謹,寫錯一個引號都不行
                    secureuri: false, //一般設置為false
                    fileElementId: 'file1', //文件上傳空間的id屬性  <input type="file" id="file" name="file" />
                    dataType: 'json', //返回值類型 一般設置為json
                    success: function (data, status)  //服務器成功響應處理函數
                    {
                        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)//服務器響應失敗處理函數
                    {
                        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>

 

此實例在顯示出異步上傳圖片的同時并彈出自定義傳輸的參數。本實例

2013年1月28日,今天調試過程中發現一個問題,就是作為文件域(<input type="file">)必須要有name屬性,如果沒有name屬性,上傳之后服務器是獲取不到圖片的。如:正確的寫法是<input type="file" id="file1" name="file1" />

2013年1月28日,最經典的錯誤終于找到原因所在了。Object function (a,b){return new e.fn.init(a,b,h)} has no method 'handleError',這個是google瀏覽器報的錯誤,非常經典, 不知道是我的版本問題還是真正存在的問題。這個問題的根源經過N次上傳才找到問題的根本所在。答案是:dataType參數一定要大寫。如:dataType: 'HTML'。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 漳州市| 淮滨县| 南皮县| 华容县| 灵寿县| 南江县| 重庆市| 二连浩特市| 孙吴县| 宁德市| 邹城市| 大兴区| 唐山市| 张掖市| 大关县| 威海市| 永寿县| 淅川县| 盐池县| 柘城县| 都安| 兴山县| 扎囊县| 延吉市| 万源市| 高邮市| 郓城县| 佛教| 大城县| 博乐市| 于田县| 祁阳县| 北宁市| 仁寿县| 和平县| 白城市| 清河县| 阿勒泰市| 锡林郭勒盟| 和林格尔县| 武穴市|