首頁 > web前端 > js教程 > 主體

jQuery插件之ajaxFileUpload

黄舟
發布: 2016-12-13 11:41:22
原創
915 人瀏覽過

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 misstate:
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>
    
登入後複製

第四步:後台頁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);
        }
    }
登入後複製
此實例在顯示出非同步上傳圖片的同時並彈出自訂傳輸的參數。

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!