PDA

View Full Version : IFrame file Download Ajax



tanujkhanna9
8 Mar 2013, 1:22 AM
Hi
I have been trying to Download a file from Servlet.

For this purpose I am using Iframe,

following below is my UI code for the same:

Ext.create('Ext.Button', {
renderTo:Ext.getBody(),
text: 'Export',
handler: function() {
Ext.Ajax.request({
url : 'csvExport',
method:'POST',
scope : this,
//method to call when the request is successful
success : function(response,action){
debugger;
var url = response.Response_text;
try {
Ext.destroy(Ext.get('testIframe'));
}
catch(e) { }
Ext.DomHelper.append(document.body, {
tag: 'iframe',
id:'testIframe',
frameBorder: 0,
width: 0,
height: 0,
src:url,
css: 'display:none;visibility:hidden;height:0px;'
});
}
});
}
});
});


And below is the server code for the doPost in java

StringBuilder filePath = new StringBuilder();
filePath.append(this.getServletContext().getRealPath(File.separator));
File file = new File(filePath.toString()+"test_trial1.csv");

System.out.println("File created here "+filePath.toString()+"test_trial.csv");

file.createNewFile();
PrintWriter out = response.getWriter();

response.setStatus(200);

response.setContentType("application/json");

String fileUrl = "http://"+ request.getServerName() +":"+ request.getServerPort() + request.getContextPath() + "/" + "test_trial1.csv";
System.out.println("URL now is "+fileUrl);
out.write(fileUrl);
out.close();


Now, server responds well with the url from where one can download the file,
but the iFrame is appended to the Body of the HTML document which is not visible, and neither does the download pop-up appears.

If approach is wrong, please correct me.

it's urgent