欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2014年06月26日 00:33:18   投稿:mdxy-dxy  
這篇文章主要介紹了一個(gè)簡(jiǎn)單的jQuery插件ajaxfileupload.js實(shí)現(xiàn)ajax上傳文件例子,需要的朋友可以參考下

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

使用說(shuō)明

需要使用jQuery庫(kù)文件 和AjaxFileUpload庫(kù)文件

使用實(shí)例

一,包含文件部分

復(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>

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

<form name="form" action="" method="POST" enctype="multipart/form-data">
……相關(guān)html代碼……
</form>
因?yàn)锳jaxFileUpload插件會(huì)自動(dòng)生成一個(gè)form提交表單。

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

三,javascript部分

<script type="text/javascript"> 
function ajaxFileUpload (){ 
loading();//動(dòng)態(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ù)說(shuō)明:
1,url表示處理文件上傳操作的文件路徑,可以測(cè)試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ù)

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

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

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

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

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

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

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

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

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)友的補(bǔ)充:

頁(yè)面代碼:

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

<html>
    <!-- 引入相關(guān)的js文件,相對(duì)路徑  -->
    <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語(yǔ)句塊的用法
                {     
                    $('#result').html('添加成功');
                },
                error: function (data, status, e)            //相當(dāng)于java中catch語(yǔ)句塊的用法
                {
                    $('#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());            //指定文件存儲(chǔ)的路徑和文件名
            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;
    }
}

相關(guān)文章

最新評(píng)論