我使用ExtJS爲我的程序構建客戶端。有一種情況是我想向服務器發送Ajax請求,並獲取響應文件(二進制文件,而不是純文本文件,即XLS或PDF)。我怎樣才能通過ExtJS獲得返回的文件(我的意思是這個文件可以下載並存儲到客戶端)?我無法使用var result = Ext.decode(response.responseText)
來接收結果,因爲響應包含二進制數據並且無法解碼。獲取響應文件的使用ExtJS
AJAX調用非常簡單:
Ext.Ajax.request({
url : 'myController/exportFile',
method : 'GET',
success : function(response, opts) {
// What should I do to get the file?
},
failure : function(response, opts) {
alert('Export file failed!')
}
});
這裏是我的服務器行動回報文件:
public void sendFile(HttpServletResponse response, String filePath) {
def file = new File(filePath);
response.setContentType("application/octet-stream");
response.setHeader("Content-disposition", "attachment;filename=${file.getName()}");
response.outputStream << file.newInputStream();
}
太謝謝你了!
很好的答案!你是最棒的 !!我已經找了很久,只有這個帖子給了我適當的答案! – davs 2011-07-28 12:47:57
@amol你能幫我嗎[這裏](http://stackoverflow.com/questions/8505995/itextsharp-generated-pdf-how-to-prompt-a-user-to-choose-where-to-save) – Armance 2011-12-15 09:53:15