struts2+jquery.form.js檔案上傳注意事項

來源:互聯網
上載者:User

///////1,js擷取上傳檔案屬性
var fso = new ActiveXObject("Scripting.FileSystemObject");
var filesize = fso.GetFile(file).size;
//ActiveXObject 是ie瀏覽器支援的特性,其他瀏覽器不適用
//在使用的時候,需要將所在網站設為可信網站,並將安全層級調低
//其實這麼做的目的是 Internet 選項--》自訂層級 中 對ActiveX 控制項和外掛程式中的選項設定成啟用或提示


///////2,用Jquery.form.js實現ajax檔案上傳(包含文本和檔案的混合模式)
//實現原理:form中嵌套iframe的方式進行提交
var options = {
async:false,
url:"",
type:"post",
//告訴jquery,提交資料包含file屬性
enctype:"multipart/form-data",
//統一字元編碼,防止亂碼
contentType:"charset=utf-8",
success: function(data) {
alert(data.resultmsg);
$('#upform').dialog("close");
},
error: function(data) {
$('#upfilebtn').attr("disabled","");
alert("上傳附件失敗,請重新選擇附件");
},
dataType: "json"
};
$("#upform").ajaxSubmit(options);

//在IE8卻發現提示下載檔案。
//IE 不支援 contentType 為application/json,在 ajax 上傳檔案完成後返回 json時 IE 提示下載檔案。
//jquery利用的是iframe模式下無重新整理上傳,而iframe並不能直接擷取返回json對象,返回的是一個對象資料流。
//在jquery.form.js這個外掛程式中它的實現是利用返回的responseText來處理。也就是說,你要是返回一個text/html的contentType,那它就
//能接收到,並且對這個返回的資料進行處理。因此,我們返回的不應該是一個json對象,而是一個json字串!
//所以在java端響應時,我們應該添加


//防止中文亂碼
ServletActionContext.getRequest().setCharacterEncoding("utf-8");
//設定ContentType:text/html,讓js外掛程式能夠處理
ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.