ajaxFileUpload 异步上传文件简单使用
copy
- <%@pagelanguage="java"contentType="text/html;charset=UTF-8"
- pageEncoding="UTF-8"%>
- <!DOCTYPEhtmlPUBLIC"-//W3C//DTDHTML4.01Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
- <htmlheadMetahttp-equiv="Content-Type"content="text/html;charset=UTF-8"title>Inserttitlehere</
- <!--引用jquery-->
- scriptsrc="//code.jquery.com/jquery-1.11.0.min.js">scriptscriptsrc="//code.jquery.com/jquery-migrate-1.2.1.min.js"<!--引用ajaxfileupload.js-->
- scriptsrc="../js/ajaxfileupload.js"
- scripttype="text/javascript" $(function(){
- //点击打开文件选择器
- $("#upload").on('click',function(){
- $('#fileToUpload').click();
- });
- //选择文件之后执行上传
- $('#fileToUpload').on('change',function(){
- $.ajaxFileUpload({
- url:'../FileUploadServlet',
- secureuri:false,
- fileElementId:'fileToUpload',//file标签的id
- dataType:'json',//返回数据的类型
- data:{name:'logan'},//一同上传的数据
- success:function(data,status){
- //把图片替换
- varobj=jQuery.parseJSON(data);
- $("#upload").attr("src","../image/"+obj.fileName);
- if(typeof(data.error)!='undefined'){
- if(data.error!=''){
- alert(data.error);
- }else{
- alert(data.msg);
- }
- }
- },248); line-height:18px; margin-top:0px!important; margin-right:0px!important; margin-bottom:0px!important; padding:0px 3px 0px 10px!important"> error:function(data,status,e){
- alert(e);
- });
- body <!--点击图片,打开文件选择器,确定,上传。(这是网络上的一个图片)-->
- imgid="upload"alt=""style="width:200px;height:200px"
- src="http://d.pcs.baidu.com/thumbnail/e8119cd92364a9b2714ea0a92af15aec?fid=2399642819-250528-305026848845811&time=1405674000&sign=FDTAER-DCb740ccc5511e5e8fedcff06b081203-abo3xnZkLb7yMEPLDWiuaQI8kXM%3D&rt=sh&expires=2h&r=900585425&sharesign=unknown&size=c710_u500&quality=100"<!--隐藏file标签-->
- inputid="fileToUpload"style="display:none"type="file"name="upfile"br/>
- >