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

首頁 > 編程 > JavaScript > 正文

一個簡單的jQuery插件ajaxfileupload.js實現(xiàn)ajax上傳文件例子

2019-11-20 14:24:04
字體:
供稿:網(wǎng)友

jQuery插件AjaxFileUpload可以實現(xiàn)ajax文件上傳,該插件使用非常簡單,首先了解一下正確使用AjaxFileUpload插件的方法,然后再了解一些常見的錯誤信息和解決方法。

使用說明

需要使用jQuery庫文件 和AjaxFileUpload庫文件

使用實例

一,包含文件部分

復(fù)制代碼 代碼如下:

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

二,HTML部分

復(fù)制代碼 代碼如下:

<img id="loading " src="loading.gif" style="display:none;">
<input id="fileToUpload " type="file" size="20" name="fileToUpload " class="input">
<button class="button" id="buttonUpload" onclick="return ajaxFileUpload ();">上傳</button>

只需要三個元素,一個動態(tài)加載小圖標(biāo)、一個文件域和一個按鈕
注意:使用AjaxFileUpload插件上傳文件可不需要form,如下:

<form name="form" action="" method="POST" enctype="multipart/form-data">
……相關(guān)html代碼……
</form>
因為AjaxFileUpload插件會自動生成一個form提交表單。

對于file文件域ID和name,ajaxFileUpload插件fileElementId參數(shù)需要獲取文件域ID,如果處理上傳文件操作就需要知道文件域name,以便獲取上傳文件信息,這兩者關(guān)系一定要清楚。

三,javascript部分

<script type="text/javascript"> function ajaxFileUpload (){ loading();//動態(tài)加載小圖標(biāo) $.ajaxFileUpload ({ url :'upload.php', secureuri :false, fileElementId :'fileToUpload', dataType : 'json', success : function (data, status){ if(typeof(data.error) != 'undefined'){ if(data.error != ''){ alert(data.error); }else{ alert(data.msg); } } }, error: function (data, status, e){ alert(e); } }) return false; } function loading (){ $("#loading ").ajaxStart(function(){ $(this).show(); }).ajaxComplete(function(){ $(this).hide(); }); } </script> 

主要參數(shù)說明:
1,url表示處理文件上傳操作的文件路徑,可以測試URL是否能在瀏覽器中直接訪問,如上:upload.php
2,fileElementId表示文件域ID,如上:fileToUpload
3,secureuri是否啟用安全提交,默認(rèn)為false
4,dataType數(shù)據(jù)數(shù)據(jù),一般選json,javascript的原生態(tài)
5,success提交成功后處理函數(shù)
6,error提交失敗處理函數(shù)

上面有兩個方法,一個動態(tài)加載小圖標(biāo)提示函數(shù)loading()和ajaxFileUpload文件上傳$.ajaxFileUpload()函數(shù),這與我們使用jQuery.ajax()函數(shù)差不多,使用很簡單,這里我省略了PHP處理上傳文件,使用jQuery插件 AjaxFileUpload實現(xiàn)ajax文件就這么簡單。

同時我們需要了解相關(guān)的錯誤提示

1,SyntaxError: missing ; before statement錯誤
如果出現(xiàn)這個錯誤就需要檢查url路徑是否可以訪問

2,SyntaxError: syntax error錯誤
如果出現(xiàn)這個錯誤就需要檢查處理提交操作的PHP文件是否存在語法錯誤

3,SyntaxError: invalid property id錯誤
如果出現(xiàn)這個錯誤就需要檢查屬性ID是否存在

4,SyntaxError: missing } in XML expression錯誤
如果出現(xiàn)這個錯誤就需要檢查文件域名稱是否一致或不存在

5,其它自定義錯誤
大家可使用變量$error直接打印的方法檢查各參數(shù)是否正確,比起上面這些無效的錯誤提示還是方便很多。

使用jQuery插件AjaxFileUpload實現(xiàn)無刷新上傳文件非常實用,由于其簡單易用,因些這個插件相比其它文件上傳插件使用人數(shù)最多,非常值得推薦。
 
處理頁面:

using System;using System.Collections;using System.Configuration;using System.Data;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.HtmlControls;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;public partial class web_ajax_FileUpload : System.Web.UI.Page{ protected void Page_Load(object sender, EventArgs e) {  HttpFileCollection files = HttpContext.Current.Request.Files;  //if (files[0].ContentLength > 5)  //{  // Response.Write("{");  // Response.Write("msg:'" + files[0].FileName + "',");  // Response.Write("error:'文件上傳失敗'");  // Response.Write("}");  //}  //else  //{  // Response.Write("{");  // Response.Write("msg:'沒有文件被上傳',");  // Response.Write("error:'文件上傳失敗'");  // Response.Write("}");  //}  files[0].SaveAs("d:/adw.jpg");  Response.Write("{");  Response.Write("msg:'a',");  Response.Write("error:''");  Response.Write("}");  //Response.Write("{");  //Response.Write("msg:'ggg/n',");  //Response.Write("error:'aa/n'");  //Response.Write("}");  Response.End(); }}

其它網(wǎng)友的補充:

頁面代碼:

復(fù)制代碼 代碼如下:

<html>
    <!-- 引入相關(guān)的js文件,相對路徑  -->
    <script type="text/javascript" src="js/jquery.js"></script>
      <script type="text/javascript" src="js/ajaxfileupload.js"></script>

    <!-- 執(zhí)行上傳文件操作的函數(shù) -->
      <script type="text/javascript">
          function ajaxFileUpload(){
               $.ajaxFileUpload(
                   {
                url:'update.do?method=uploader',            //需要鏈接到服務(wù)器地址
                secureuri:false,
                fileElementId:'houseMaps',                        //文件選擇框的id屬性
                dataType: 'xml',                                     //服務(wù)器返回的格式,可以是json
                success: function (data, status)            //相當(dāng)于java中try語句塊的用法
                {     
                    $('#result').html('添加成功');
                },
                error: function (data, status, e)            //相當(dāng)于java中catch語句塊的用法
                {
                    $('#result').html('添加失敗');
                }
            }
                  
               );
             
          }
      </script>
  </head> 
  <body>
      <form method="post" action="update.do?method=uploader" enctype="multipart/form-data"> 
        <input type="file" id="houseMaps" name="houseMaps"/>
        <input type="button" value="提交" onclick="ajaxFileUpload()"/>
    </form>
    <div id="result"></div>
   
  </body>
</html>

服務(wù)器代碼:

復(fù)制代碼 代碼如下:

public class UpdateAction extends DispatchAction {

    public ActionForward uploader(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) {
        UpFormForm upFormForm = (UpFormForm) form;
        FormFile ff = upFormForm.getHouseMaps();
        try {
            InputStream is = ff.getInputStream();
            File file = new File("D:/" + ff.getFileName());            //指定文件存儲的路徑和文件名
            OutputStream os = new FileOutputStream(file);
           
            byte[] b = new byte[1024];
            int len = 0;
            while((len = is.read(b)) != -1){
                os.write(b, 0, len);
            }
            os.close();
            is.close();
        } catch (Exception e) {
            e.printStackTrace();
           
        }
       
        return null;
    }
}

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 唐海县| 陇川县| 无棣县| 揭西县| 巩义市| 琼海市| 南溪县| 勐海县| 陆川县| 龙岩市| 南岸区| 梁山县| 乾安县| 永寿县| 蚌埠市| 临沂市| 徐州市| 抚宁县| 青川县| 资溪县| 郯城县| 察隅县| 吴江市| 柞水县| 乌审旗| 射洪县| 大埔县| 康平县| 绩溪县| 邛崃市| 思茅市| 香格里拉县| 新源县| 土默特左旗| 若尔盖县| 延安市| 大庆市| 广水市| 北京市| 鄂尔多斯市| 南岸区|