PDA

View Full Version : Download file from servlet with Ajax



Ange5545
22 Nov 2013, 3:58 AM
Hello everybody.

Today is five day, when I try to download my file from server with ExtJS using...
Tomorrow start weekend but there are not success in my work, since the beginning of the week.

I have servlet. And it creating the .xls file and send it to user.
.

//---xls-file saving---


response.setContentType("application/vnd.ms-excel");


// gets MIME type of the file
String mimeType = "application/vnd.ms-excel";
// forces download
String headerKey = "Content-Disposition";
String headerValue = String.format("attachment; filename=\"%s\"",
"FileName.xls");
response.setHeader(headerKey, headerValue);
//
OutputStream outStream = response.getOutputStream();
wb.write(outStream);
outStream.close();



If I get this file from html page I havent the trouble. But I use ExtJS...

I try catch this file with iframe in Ajax:




Ext.Ajax.request({
url : '/myServlet',
method:'POST',
contentType: "application/vnd.ms-excel", // I dont know Nedd it or not
success : function(response,action){
var url = response.responseText; // I dont understand what I must write here
//Ext.Msg.alert('URL is', url);

// creating iframe
var el = document.createElement('iframe');
document.body.appendChild(el);
el.id = 'iframe';
el.src = url; // And here
})

params: { 'MyParametr': myPar }
});


I think what me need send the response in my frame, but if I send simple "pesponce" or "response.responseText" it dont work.

In this forum there are a lot of example for this. But I cannot get it work. Please tell me What I need to do in this code, that I can get a my file??

(Sorry for my bad English)

mitchellsimoens
3 Dec 2013, 1:11 PM
You cannot use javascript to prompt for download. You can use Flash or use headers from the server.

Ange5545
14 Jan 2014, 12:34 AM
I solve it, but without "Ext.Ajax".
I create iframe in new ext window, and get my params in it SRC.thank you for yor care)

venkikodakirthi
21 Jan 2014, 2:57 PM
Hi Ange5545, Can you share your solution, how you solved without Ext.Ajax?