怎么讓這個內容被瀏覽器解析?各位老大請幫個忙public void downLoad(String filePath, HttpServletResponse response,) throws Exception{File f = new File(filePath);if (!f.exists()){response.sendError(404, "File not found!");return;}BufferedInputStream br = new BufferedInputStream(new FileInputStream(f));byte[] buf = new byte[1024];int len = 0;response.reset();response.setContentType("application/x-msdownload");response.addHeader("Content-Disposition", "attachment; filename="+ new String(f.getName().getBytes("GBK"), "ISO8859-1"));OutputStream out = response.getOutputStream();while ((len = br.read(buf)) > 0){out.write(buf, 0, len);}br.close();out.flush();out.close();}
2 回答

弒天下
TA貢獻1818條經驗 獲得超8個贊
1、既然是文件下載就不要用Ajax請求,最好用JS直接打開地址的方法來響應JS事件。
2、其次你的Servlet代碼里面要設置正確的ContentType和Content-Disposition的attachment header。

HUWWW
TA貢獻1874條經驗 獲得超12個贊
在ajax的回調函數里,用類似這樣的代碼:
alert(ajax.responseText);
或者:
var show = document.createElement('div');
show.innerHTML = ajax.responseText;
document.body.appendChild(show);
這樣就把內容顯示到頁面中了。
如果是把ajax的東西下載到本地:
如果是給特定用戶使用的,可以考慮用IE的new ActiveObject函數調用本地的自動化控件。如果是給一般的瀏覽器用,是無法寫入到本地的。基本上上你要瀏覽器下載東西,要么是a標簽一類的,要么是你更改location,這個是要刷新頁面的。
總之你ajax得到的東西,沒有通用的方法可以保存成本地文件,最多借助一些RIA的東西。
添加回答
舉報
0/150
提交
取消