jQuery异步上传文件插件ajaxFileUpload详细介绍
一、ajaxFileUpload是一个异步上传文件的jQuery插件。
传一个不知道什么版本的上来,以后不用到处找了。
语法:$.ajaxFileUpload([options])
options参数说明:
1、url 上传处理程序地址。
2,fileElementId 需要上传的文件域的ID,即<inputtype="file">的ID。
3,secureuri是否启用安全提交,默认为false。
4,dataType服务器返回的数据类型。可以为xml,script,json,html。如果不填写,jQuery会自动判断。
5,success提交成功后自动执行的处理函数,参数data就是服务器返回的数据。
6,error提交失败自动执行的处理函数。
7,data 自定义参数。这个东西比较有用,当有数据是与上传的图片相关的时候,这个东西就要用到了。
8,type 当要提交自定义参数时,这个参数要设置成post
错误提示:
1,SyntaxError:missing;beforestatement错误
如果出现这个错误就需要检查url路径是否可以访问
2,SyntaxError:syntaxerror错误
如果出现这个错误就需要检查处理提交操作的服务器后台处理程序是否存在语法错误
3,SyntaxError:invalidpropertyid错误
如果出现这个错误就需要检查文本域属性ID是否存在
4,SyntaxError:missing}inXMLexpression错误
如果出现这个错误就需要检查文件name是否一致或不存在
5,其它自定义错误
大家可使用变量$error直接打印的方法检查各参数是否正确,比起上面这些无效的错误提示还是方便很多。
使用方法:
第一步:先引入jQuery与ajaxFileUpload插件。注意先后顺序,这个不用说了,所有的插件都是这样。
<scriptsrc="jquery-1.7.1.js"type="text/javascript"></script> <scriptsrc="ajaxfileupload.js"type="text/javascript"></script>
第二步:HTML代码:
<body> <p><inputtype="file"id="file1"name="file"/></p> <inputtype="button"value="上传"/> <p><imgid="img1"alt="上传成功啦"src=""/></p> </body>
第三步:JS代码
<scriptsrc="jquery-1.7.1.js"type="text/javascript"></script>
<scriptsrc="ajaxfileupload.js"type="text/javascript"></script>
<scripttype="text/javascript">
$(function(){
$(":button").click(function(){
ajaxFileUpload();
})
})
functionajaxFileUpload(){
$.ajaxFileUpload
(
{
url:'/upload.aspx',//用于文件上传的服务器端请求地址
secureuri:false,//是否需要安全协议,一般设置为false
fileElementId:'file1',//文件上传域的ID
dataType:'json',//返回值类型一般设置为json
success:function(data,status) //服务器成功响应处理函数
{
$("#img1").attr("src",data.imgurl);
if(typeof(data.error)!='undefined'){
if(data.error!=''){
alert(data.error);
}else{
alert(data.msg);
}
}
},
error:function(data,status,e)//服务器响应失败处理函数
{
alert(e);
}
}
)
returnfalse;
}
</script>
第四步:后台页面upload.aspx代码:
protectedvoidPage_Load(objectsender,EventArgse)
{
HttpFileCollectionfiles=Request.Files;
stringmsg=string.Empty;
stringerror=string.Empty;
stringimgurl;
if(files.Count>0)
{
files[0].SaveAs(Server.MapPath("/")+System.IO.Path.GetFileName(files[0].FileName));
msg="成功!文件大小为:"+files[0].ContentLength;
imgurl="/"+files[0].FileName;
stringres="{error:'"+error+"',msg:'"+msg+"',imgurl:'"+imgurl+"'}";
Response.Write(res);
Response.End();
}
}
本实例完整代码下载
来一个MVC版本的实例:
控制器代码
publicclassHomeController:Controller
{
publicActionResultIndex()
{
returnView();
}
publicActionResultUpload() { HttpFileCollectionhfc=System.Web.HttpContext.Current.Request.Files; stringimgPath=""; if(hfc.Count>0) { imgPath="/testUpload"+hfc[0].FileName; stringPhysicalPath=Server.MapPath(imgPath); hfc[0].SaveAs(PhysicalPath); } returnContent(imgPath); } }