一個(gè)簡(jiǎn)單的jQuery插件ajaxfileupload.js實(shí)現(xiàn)ajax上傳文件例子
jQuery插件AjaxFileUpload可以實(shí)現(xiàn)ajax文件上傳,該插件使用非常簡(jiǎn)單,首先了解一下正確使用AjaxFileUpload插件的方法,然后再了解一些常見(jiàn)的錯(cuò)誤信息和解決方法。
使用說(shuō)明
需要使用jQuery庫(kù)文件 和AjaxFileUpload庫(kù)文件
使用實(shí)例
一,包含文件部分
<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>
只需要三個(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是否能在瀏覽器中直接訪問(wèn),如上: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路徑是否可以訪問(wèn)
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:'沒(méi)有文件被上傳',");
// 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è)面代碼:
<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ù)器代碼:
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;
}
}
- jQuery Ajax 上傳文件處理方式介紹(推薦)
- JQuery.uploadify 上傳文件插件的使用詳解 for ASP.NET
- jquery實(shí)現(xiàn)兼容IE8的異步上傳文件
- jQuery實(shí)現(xiàn)jQuery-form.js實(shí)現(xiàn)異步上傳文件
- jQuery插件ajaxFileUpload異步上傳文件
- 基于jQuery Ajax實(shí)現(xiàn)上傳文件
- jquery上傳插件fineuploader上傳文件使用方法(jquery圖片上傳插件)
- jQuery Ajax使用FormData對(duì)象上傳文件的方法
- jQuery簡(jiǎn)單驗(yàn)證上傳文件大小及類(lèi)型的方法
- Jquery實(shí)現(xiàn)異步上傳文件
相關(guān)文章
jQuery對(duì)checkbox 復(fù)選框的全選全不選反選的操作
這篇文章主要介紹了jQuery對(duì)checkbox 復(fù)選框的全選全不選反選的操作 的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-08-08
jQuery+datatables插件實(shí)現(xiàn)ajax加載數(shù)據(jù)與增刪改查功能示例
這篇文章主要介紹了jQuery+datatables插件實(shí)現(xiàn)ajax加載數(shù)據(jù)與增刪改查功能,涉及jQuery結(jié)合datatables插件針對(duì)頁(yè)面表格實(shí)現(xiàn)數(shù)據(jù)加載及增刪改查等相關(guān)操作技巧,需要的朋友可以參考下2018-04-04
jQuery1.3.2 升級(jí)到j(luò)Query1.4.4需要修改的地方
jQuery1.3.2 升級(jí)到 1.4.4 ,需要修改的地方,需要的朋友可以參考下。2011-01-01
jQuery實(shí)現(xiàn)的placeholder效果完整實(shí)例
這篇文章主要介紹了jQuery實(shí)現(xiàn)的placeholder效果,可實(shí)現(xiàn)輸入框提示文字的功能,并且針對(duì)焦點(diǎn)的情況判定是否顯示,非常簡(jiǎn)單實(shí)用,需要的朋友可以參考下2016-08-08
jQuery統(tǒng)計(jì)指定子元素?cái)?shù)量的方法
這篇文章主要介紹了jQuery統(tǒng)計(jì)指定子元素?cái)?shù)量的方法,涉及jQuery可以通過(guò)>訪問(wèn)子標(biāo)簽的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-03-03
jquery實(shí)現(xiàn)簡(jiǎn)單的輪換出現(xiàn)效果實(shí)例
這篇文章主要介紹了jquery實(shí)現(xiàn)簡(jiǎn)單的輪換出現(xiàn)效果,涉及jquery針對(duì)圖片樣式切換效果的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-07-07

