首頁  >  文章  >  php教程  >  jQuery插件之ajaxFileUpload

jQuery插件之ajaxFileUpload

高洛峰
高洛峰原創
2016-12-12 17:29:271594瀏覽

ajaxFileUpload.js 很多同名的,因為做出來一個很容易。

我用的是這個:https://github.com/carlcarl/AjaxFileUpload 

下載地址在這裡:http://files.cnblogs.com/files/kissdodog/ajaxfileupload_JS_File.rar

一個很有名的插件,只是別人寫好的放出來供大家用,原理都是創建隱藏的表單和iframe然後用JS去提交,獲得回傳值。

當初做了個非同步上傳的功能,選擇它因為它的配置方式比較像jQuery的AJAX,我很喜歡。

評論裡面說到的不行。那是因為我們用的不是同一個js。我上github搜尋AjaxFileUpload出來很多類似js。

ajaxFileUpload是一個非同步上傳檔案的jQuery插件

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

  語法:$.ajaxFileUpload([options])

  options參數說明:

1、url                、                 〴                       無無合子處理程序。  

2,fileElementId       需要上傳的檔案域的ID,即的ID。
3,secureuri        是否啟用安全提交,預設為false。 
4,dataType        伺服器傳回的資料類型。可以是xml,script,json,html。如果不填寫,jQuery會自動判斷。
5,success        提交成功後自動執行的處理函數,參數data就是伺服器傳回的資料。
6,error          提交失敗自動執行的處理函數。
7,data           自訂參數。這個東西比較有用,當有數據是跟上傳的圖片相關的時候,這個東西就要用到了。
8, type            當要提交自訂參數時,這個參數要設定成post

錯誤提示:

1,SyntaxError: missing fefor:

SyntaxError: syntax error錯誤
  如果出現這個錯誤就需要檢查處理提交操作的伺服器後台處理程序是否存在語法錯誤
3,SyntaxError: invalid property id錯誤
  如果出現這個錯誤就需要檢查文本域屬性是否存在,SyntaxError: missing } in XML expression錯誤
  如果出現這個錯誤就需要檢查文件name是否一致或不存在
5,其它自訂錯誤
  上面這些無效的錯誤提示還是方便很多。

 

使用方法:

第一步:先引入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: &#39;/upload.aspx&#39;, //用于文件上传的服务器端请求地址
                    secureuri: false, //是否需要安全协议,一般设置为false
                    fileElementId: &#39;file1&#39;, //文件上传域的ID
                    dataType: &#39;json&#39;, //返回值类型 一般设置为json
                    success: function (data, status)  //服务器成功响应处理函数
                    {
                        $("#img1").attr("src", data.imgurl);
                        if (typeof (data.error) != &#39;undefined&#39;) {
                            if (data.error != &#39;&#39;) {
                                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:&#39;" + error + "&#39;, msg:&#39;" + msg + "&#39;,imgurl:&#39;" + imgurl + "&#39;}";
                Response.Write(res);
                Response.End();
            }
        }

來一個MVC版本的實例:

前端視圖,HTML與JS程式碼,成功上傳後,返回圖片真實地址並綁定到jQuery插件之ajaxFileUpload的SRC地址

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);
        }
    }

最後再來一個上傳圖片且附帶參數的實例:控制器代碼:



    
    
    

<body>
    <p><input type="file" id="file1" name="file" /></p>
    <input type="button" value="上传" />
    <p><img id="img1" alt="上传成功啦" src="" /></p>
</body>

Index視圖代碼:

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);
        }
    }

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn