问题
I make an ExtJS AJAX request and I want the standard browser 'save as' dialog box to be displayed to the user. The file to download can be a pdf, jpg or png file.
At the moment the AJAX request is successfully submitted but as it's asynchronous no dialog box comes up.
I could simply not use AJAX but there is a lot of javascript code that I need to use prior to making the request and I don't really feel rewriting it to java.
My code looks like this:
var params = getPrintInfo(form);
Ext.Ajax.request({
url : 'print',
method : 'POST',
params : {
customData: params.customData,
dpi: params.dpi,
format: params.format,
grid: params.grid,
title: params.title
},
autoAbort : false,
success : function(result, request) {
if(result.responseText==''){
//display error message
}else{
// display save as dialog box
}
}
});
In my controller I'm setting the headers to be:
httpResponse.setHeader("Content-disposition", "attachment; filename=" + this.config.getString("print.file.name")+outputType);
EDIT:
Have actually found this solution:
Ext.DomHelper.append(document.body, {
tag: 'iframe',
frameBorder: 0,
width: 0,
height: 0,
css: 'display:none;visibility:hidden;height:1px;',
src: 'http://blabla.com/f75e927b-2041-473e-86ba-cbbc60dbc285.pdf'
});
Now the question is: How can I change the pdf name to be pretier, for example map.pdf instead of having that long ugly alphanumeric string?
回答1:
You can use window.open to have save as dialog box, for example
window.open('print?parameters...','_blank');
来源:https://stackoverflow.com/questions/9467367/extjs-ajax-save-as-dialog-box