jQuery外掛程式AjaxFileUpload可以實現ajax檔案上傳,該外掛程式使用非常簡單,首先瞭解一下正確使用AjaxFileUpload外掛程式的方法,然後再瞭解一些常見的錯誤資訊和解決方案。
使用說明
需要使用jQuery庫檔案 和AjaxFileUpload庫檔案
使用執行個體
一,包含檔案部分
複製代碼 代碼如下:
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="ajaxfileupload.js"></script>
二,HTML部分
複製代碼 代碼如下:
<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>
只需要三個元素,一個動態載入小表徵圖、一個檔案域和一個按鈕
注意:使用AjaxFileUpload外掛程式上傳檔案可不需要form,如下:
<form name="form" action="" method="POST" enctype="multipart/form-data">
……相關html代碼……
</form>
因為AjaxFileUpload外掛程式會自動產生一個form提交表單。
對於file檔案域ID和name,ajaxFileUpload外掛程式fileElementId參數需要擷取檔案域ID,如果處理上傳檔案操作就需要知道檔案域name,以便擷取上傳檔案資訊,這兩者關係一定要清楚。
三,javascript部分
<script type="text/javascript"> function ajaxFileUpload (){ loading();//動態載入小表徵圖 $.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>
主要參數說明:
1,url表示處理檔案上傳操作的檔案路徑,可以測試URL是否能在瀏覽器中直接存取,如上:upload.php
2,fileElementId表示檔案域ID,如上:fileToUpload
3,secureuri是否啟用安全提交,預設為false
4,dataType資料資料,一般選json,javascript的原生態
5,success提交成功後處理函數
6,error提交失敗處理函數
上面有兩個方法,一個動態載入小表徵圖提示函數loading()和ajaxFileUpload檔案上傳$.ajaxFileUpload()函數,這與我們使用jQuery.ajax()函數差不多,使用很簡單,這裡我省略了PHP處理上傳檔案,使用jQuery外掛程式 AjaxFileUpload實現ajax檔案就這麼簡單。
同時我們需要瞭解相關的錯誤提示
1,SyntaxError: missing ; before statement錯誤
如果出現這個錯誤就需要檢查url路徑是否可以訪問
2,SyntaxError: syntax error錯誤
如果出現這個錯誤就需要檢查處理提交操作的PHP檔案是否存在語法錯誤
3,SyntaxError: invalid property id錯誤
如果出現這個錯誤就需要檢查屬性ID是否存在
4,SyntaxError: missing } in XML expression錯誤
如果出現這個錯誤就需要檢查檔案網域名稱稱是否一致或不存在
5,其它自訂錯誤
大家可使用變數$error直接列印的方法檢查各參數是否正確,比起上面這些無效的錯誤提示還是方便很多。
使用jQuery外掛程式AjaxFileUpload實現無重新整理上傳檔案非常實用,由於其簡單易用,因些這個外掛程式相比其它檔案上傳外掛程式使用人數最多,非常值得推薦。
處理頁面:
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(); }}
其它網友的補充:
頁面代碼:
複製代碼 代碼如下:
<html>
<!-- 引入相關的js檔案,相對路徑 -->
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/ajaxfileupload.js"></script>
<!-- 執行上傳檔案操作的函數 -->
<script type="text/javascript">
function ajaxFileUpload(){
$.ajaxFileUpload(
{
url:'update.do?method=uploader', //需要連結到伺服器位址
secureuri:false,
fileElementId:'houseMaps', //檔案選擇框的id屬性
dataType: 'xml', //伺服器返回的格式,可以是json
success: function (data, status) //相當於java中try語句塊的用法
{
$('#result').html('添加成功');
},
error: function (data, status, e) //相當於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>
伺服器代碼:
複製代碼 代碼如下:
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;
}
}