程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> JSP編程 >> 關於JSP >> JSP使用ajaxFileUpload.js實現跨域問題,ajaxfileupload跨域

JSP使用ajaxFileUpload.js實現跨域問題,ajaxfileupload跨域

編輯:關於JSP

JSP使用ajaxFileUpload.js實現跨域問題,ajaxfileupload跨域


廢話不多說了,直接給大家貼代碼了。

jsp代碼如下

$.ajaxFileUpload 
( 
{ 
url:'http://lh.abc.com:8080/gap/gap/fileUpload.do',//用於文件上傳的服務器端請求地址(本機為fxb.abc.com) 
secureuri:false,//一般設置為false 
fileElementId:'file',//文件上傳空間的id屬性 <input type="file" id="file" name="file" /> 
dataType: 'jsonp',//返回值類型 一般設置為json 
jsonp: 'jsoncallback', 
jsonpCallback:'success_jsonpCallback', 
function success_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); 
} 
} 
)

配置文件

<action name="fileUpload" class="com.gap.action.FileUploadAction" method="fileUpload">
<result type="json" name="success">
<param name="contentType">
text/html
</param>
</result>
<result type="json" name="error">
<param name="contentType">
text/html
</param>
</result>
</action>

action中的處理如下

public String fileUpload() throws Exception {
String path = ServletActionContext.getRequest().getRealPath("/upload1");
// String path = ConfigDataInfo.getConfigValue("imgServer");
try {
File f = this.getFile();
if (this.getFileFileName().endsWith(".exe")) {
message = "對不起,你上傳的文件格式不允許!!!";
} else {
FileInputStream inputStream = new FileInputStream(f);
FileOutputStream outputStream = new FileOutputStream(path + "/"
+ this.getFileFileName());
byte[] buf = new byte[1024];
int length = 0;
while ((length = inputStream.read(buf)) != -1) {
outputStream.write(buf, 0, length);
}
inputStream.close();
outputStream.flush();
message = "上傳成功";
}
} catch (Exception e) {
e.printStackTrace();
message = "對不起,文件上傳失敗了!!!!";
}
return SUCCESS;
}

每次跨域上傳圖片時,可以成功上傳到服務器上,但是不能正確的返回信息,總是進入error方法中,正確應該進入success方法

您可能感興趣的文章:

  • AjaxControlToolkit AjaxFileUpload 顯示英文改成中文的解決方法
  • jquery之ajaxfileupload異步上傳插件(附工程代碼)
  • 利用ajaxfileupload插件實現文件上傳無刷新的具體方法
  • jsp防止跨域提交數據的具體實現
  • 為jquery的ajaxfileupload增加附加參數的方法
  • PHP+ajaxfileupload+jcrop插件完美實現頭像上傳剪裁
  • 一個簡單的jQuery插件ajaxfileupload.js實現ajax上傳文件例子
  • 使用ajaxfileupload.js實現ajax上傳文件php版

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved