jsp頁面鏈接,點擊訪問action用io流去下載服務器上的文件,問題是任憑怎么點擊都沒反應,日志也不報錯。
前臺ajax代碼
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
ext.ajax.request({ url : '/yjy/training/trainingtimeaction.do?method=downloadattchbyid' , params : { timeid : timeids }, success : function(response,options){ var result = ext.util.json.decode(response.responsetext); ext.msg.alert( "下載成功" ); }, failure :function(response,options){ var result = ext.util.json.decode(response.responsetext); ext.msg.alert( "下載失敗" +result.message); } }); |
后臺action代碼
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
string timeid = request.getparameter( "timeid" ); string sql = "select doc_name from cper.ehrtrain_item_document where item_id = ?" ; dbhelper dbhelper = new dbhelper(); object[] params = new object[]{timeid}; string filename = (string)dbhelper.runsqlscalar(sql, params); string filepath = serverpathutil.getpathroot()+ "web-inf/cache/train_item_file/train_item_file_" +timeid+ "/" +filename; file file = new file(filepath); if (!file.exists()){ logger.debug( "文件不存在" ); throw new ioexception( "the file not exists" ); } response.setcontentlength(( int ) file.length()); outputstream o = response.getoutputstream(); byte b[] = new byte [ 5000 ]; //response.setcontenttype("application/x-msdownload"); response.setcontenttype( "application/vnd.ms-excel" ); response.setcontentlength(( int )file.length()); response.setheader( "content-disposition" , "attachment; filename=" +filename); fileinputstream in = new fileinputstream(file); int n; while ((n = in.read(b)) != - 1 ) { o.write(b, 0 , n); } in.close(); } catch (exception e){ e.printstacktrace(); } |
解決方法:文件的下載,在前臺請求的時候,只能是form表單請求,或者用window.open的方式,最后我采用了window.open的方式
1
|
window.open( '/yjy/training/trainingtimeaction.do?method=downloadattchbyid&timeid=' + timeids); |
注:采用這種方式頁面會彈出一個空白窗口,下載之后窗口自動關閉,如果不想顯示這個窗口,使用form提交的方式
總結
以上所述是小編給大家介紹的java實現文件點擊沒反應的方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!
原文鏈接:https://www.cnblogs.com/thouthout/archive/2018/07/25/9366048.html