JSP使用ajaxFileUpload.js实现跨域问题
废话不多说了,直接给大家贴代码了。
jsp代码如下
$.ajaxFileUpload
(
{
url:'http://lh.abc.com:8080/gap/gap/fileUpload.do',//用于文件上传的服务器端请求地址(本机为fxb.abc.com)
secureuri:false,//一般设置为false
fileElementId:'file',//文件上传空间的id属性<inputtype="file"id="file"name="file"/>
dataType:'jsonp',//返回值类型一般设置为json
jsonp:'jsoncallback',
jsonpCallback:'success_jsonpCallback',
functionsuccess_jsonpCallback(data){
alert("1");
},
success:function(data,status)//服务器成功响应处理函数
{
alert(data.message);//从服务器返回的json中取出message中的数据,其中message为在struts2中action中定义的成员变量
if(typeof(data.error)!='undefined')
{
if(data.error!='')
{
alert(data.error);
}else
{
alert(data.message);
}
}
},
error:function(data,status,e)//服务器响应失败处理函数
{
alert(status);
alert(e);
}
}
)
配置文件
<actionname="fileUpload"class="com.gap.action.FileUploadAction"method="fileUpload"> <resulttype="json"name="success"> <paramname="contentType"> text/html </param> </result> <resulttype="json"name="error"> <paramname="contentType"> text/html </param> </result> </action>
action中的处理如下
publicStringfileUpload()throwsException{
Stringpath=ServletActionContext.getRequest().getRealPath("/upload1");
//Stringpath=ConfigDataInfo.getConfigValue("imgServer");
try{
Filef=this.getFile();
if(this.getFileFileName().endsWith(".exe")){
message="对不起,你上传的文件格式不允许!!!";
}else{
FileInputStreaminputStream=newFileInputStream(f);
FileOutputStreamoutputStream=newFileOutputStream(path+"/"
+this.getFileFileName());
byte[]buf=newbyte[1024];
intlength=0;
while((length=inputStream.read(buf))!=-1){
outputStream.write(buf,0,length);
}
inputStream.close();
outputStream.flush();
message="上传成功";
}
}catch(Exceptione){
e.printStackTrace();
message="对不起,文件上传失败了!!!!";
}
returnSUCCESS;
}
每次跨域上传图片时,可以成功上传到服务器上,但是不能正确的返回信息,总是进入error方法中,正确应该进入success方法
热门推荐
10 对患者生日祝福语简短
11 结婚祝福语简短装备
12 周岁祝福语学生文案简短
13 订婚领证祝福语简短精辟
14 导师获奖祝福语大全简短
15 新婚购房祝福语简短精辟
16 牛年祝福语简短的爱人
17 送芒果的祝福语简短
18 送给学长毕业祝福语简短