一個簡單的jQuery插件ajaxfileupload.js實現(xiàn)ajax上傳文件例子
jQuery插件AjaxFileUpload可以實現(xiàn)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>
只需要三個元素,一個動態(tài)加載小圖標、一個文件域和一個按鈕
注意:使用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)加載小圖標
$.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是否啟用安全提交,默認為false
4,dataType數(shù)據(jù)數(shù)據(jù),一般選json,javascript的原生態(tài)
5,success提交成功后處理函數(shù)
6,error提交失敗處理函數(shù)
上面有兩個方法,一個動態(tài)加載小圖標提示函數(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)友的補充:
頁面代碼:
<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', //需要鏈接到服務器地址
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;
}
}
- jQuery Ajax 上傳文件處理方式介紹(推薦)
- JQuery.uploadify 上傳文件插件的使用詳解 for ASP.NET
- jquery實現(xiàn)兼容IE8的異步上傳文件
- jQuery實現(xiàn)jQuery-form.js實現(xiàn)異步上傳文件
- jQuery插件ajaxFileUpload異步上傳文件
- 基于jQuery Ajax實現(xiàn)上傳文件
- jquery上傳插件fineuploader上傳文件使用方法(jquery圖片上傳插件)
- jQuery Ajax使用FormData對象上傳文件的方法
- jQuery簡單驗證上傳文件大小及類型的方法
- Jquery實現(xiàn)異步上傳文件
相關(guān)文章
jquery中獲得$.ajax()事件返回的值并添加事件的方法
如果想獲得$.ajax()中返回的值,直接用在success:funciton(){return xx} 是不可以的,要想獲得xx的值,要在script中,使用全局變量。利用全局變量引出xx的值。2010-04-04
jQuery實現(xiàn)仿美橙互聯(lián)兩級導航菜單的方法
這篇文章主要介紹了jQuery實現(xiàn)仿美橙互聯(lián)兩級導航菜單的方法,實例分析了jQuery操作css及setTimeout等實現(xiàn)導航菜單的技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-03-03
jquery插件方式實現(xiàn)table查詢功能的簡單實例
下面小編就為大家?guī)硪黄猨query插件方式實現(xiàn)table查詢功能的簡單實例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-06-06

