首頁  >  文章  >  php教程  >  jQuery外掛程式ajaxFileUpload使用實例解析

jQuery外掛程式ajaxFileUpload使用實例解析

高洛峰
高洛峰原創
2016-12-09 16:01:441203瀏覽

AjaxFileUpload.js並不是一個很有名的插件,只是別人寫好的放出來供大家用,原理都是創建隱藏的表單和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、SyntanxError:misyntax〜

2、SyntaxError: syntax error錯誤
  如果出現這個錯誤就需要檢查處理提交操作的伺服器後台處理程序是否存在語法錯誤
3、SyntaxError: invalid property id錯誤
㟀 如果出現這個錯誤就存在檢查文本域屬性
4、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>

   

三步:JSloadup骨頁後第四步

<script src="jquery-1.7.1.js" type="text/javascript"></script>
<script src="ajaxfileupload.js" type="text/javascript"></script>

   

本實例完整程式碼下載

來一個MVC版本的實例:

控制器程式碼

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

   

前端視圖,HTML與JS程式碼,並上傳地址並建立真實位址>的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);
  }
}

   

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

<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: &#39;/Home/Upload&#39;, //用于文件上传的服务器端请求地址
          secureuri: false, //一般设置为false
          fileElementId: &#39;file1&#39;, //文件上传空间的id属性 <input type="file" id="file" name="file" />
          dataType: &#39;HTML&#39;, //返回值类型 一般设置为json
          success: function (data, status) //服务器成功响应处理函数
          {
            alert(data);
            $("#img1").attr("src", data);
            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>
</head>
<body>
  <p><input type="file" id="file1" name="file" /></p>
  <input type="button" value="上传" />
  <p><img id="img1" alt="上传成功啦" src="" /></p>
</body>
</html>

  

:

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

  

此實例在顯示出非同步上傳圖片的同時並彈出自訂傳輸的參數。本實例下載地址

  2013年1月28日,今天調試過程中發現一個問題,就是作為文件域()必須要有name屬性,如果沒有name屬性,上傳之後伺服器是取得不到圖片的。如:正確的寫法是

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

  2016-07-28,評論中的一個錯誤:TypeError: $.ajaxFileUpload is not a function   我們用的不是同一個JS,你​​下了別的AJAXFileUploadload去了。

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