I'm creating a workbook object(excel object "org.apache.poi.ss.usermodel.Workbook") on an ajax request.
How can I send this file from servlet to browser and let the browser pops up a download box.
You will have to set Content-Type to application/x-msdownload and Header to Content-Disposition
For example:
String fileName = URLDecoder.decode("MyBook.xlsx", "ISO8859_1");//Change if required
response.setContentType("application/x-msdownload");
response.setHeader("Content-disposition", "attachment; filename="+ fileName);
//And just write output stream here
Since you have changed the response headers so a Browser will automatically display a popup message to download the file.
After a lot of googling I get to know its difficult to do with an ajax request.
So I decided to go with hidden form.
In javascript I created a hidden form and submitted the form So I am getting the excel download pop-up.
my javascript is given below.
var form = document.createElement("form"); //created dummy form for submitting.
var element1 = document.createElement("input");
form.method = "POST";
form.action = "/my/servlet/path";
element1.value=values; //its a json string I need to pass to server.
element1.name="data";
element1.type = 'hidden'
form.appendChild(element1);
document.body.appendChild(form);
form.submit();
Related
Using my spring controller I want to directly open the printing view of generated pdf.By now I am generating a pdf using iTextPDF and put it to the OutputStream (HttpServletResponse.getOutputStream()).It is downloading the pdf. I can open it in browser and print using print button.
What I want is getting the print UI from the controller or without print UI send to the printer.
I have added some of my controller method,
String mimeType = "application/pdf";
System.out.println("MIME type: " + mimeType);
response.setContentType(mimeType);
String headerKey = "Content-Disposition";
String headerValue = String.format("theCoder379.PDF");
response.setHeader(headerKey, headerValue);
OutputStream outStream = response.getOutputStream();
createPdf(outStream, theObject);
outStream.close();
In createPdf(outStream, theObject) method it is adding the generated iText pdf using 'theObject' to the 'outStream'.
How can I achieve this.
It is not possible the way you want (from the server side).
By now I am generating a pdf using iTextPDF and put it to the OutputStream
Good. That's effectively all you can do from the server side code.
I can open it in browser and print using print button
The browser loads an external application (pdf plugin/viewer) and let the plugin display the file you've generated. You have no control over it whatsoever.
However you may create your own page with embedded plugin.
<object data="./pdfServlet" type="application/pdf" width="100%" height="100%">
<p>Alternative text - include a link to the PDF!
</p>
</object>
and try to invoke the print event in the client-side javascript.
window.print();
However - I see some potential issues
it may not work on all browsers and all version you may want
some people (myself included) are sensitive if a web page tries to do thing the are not explicitly asked for (such as printing)
The response of an AJAX response in HtmlUnit is a single div, which contains a table of data.
The response also contains a small JS script.
The problem is that HtmlUnit is trying to parse the response as a complete HTML. So, it expects that snippet to have all JS libraries like jQuery.
Is there a way to parse the snippet in the context of the parent page which fired the AJAX?
Alternatively, it would be ok if I just got the response as plain text. But, the request has to be within the session, along with all the Html headers intact.
I guess the AJAX url can't be used for getPage. Instead, I loaded the outer page once, and triggered a script on that page, which loaded the AJAX response into a div.
String jsCommand = "$('#results_box').load( '"+ pageLink +"',"+ formdata +");";
parentPage.executeJavaScript(jsCommand);
I am trying to set the Title of the Browser as the PDF document Title.
Though the pdf generates correctly, i get the Title randomly.
response.setDateHeader("Expires", 0L);
response.setHeader("Content-disposition", "inline;filename=" + title + ".pdf");
response.setContentType("application/pdf");
response.setContentLength(bArray.length);
try {
response.getOutputStream().write(bArray);
response.getOutputStream().close();}
Could someone please help me out here to override the pdf title
You cannot set the title while at the same time sending a file down to the browser. For the browser to change the "page" title, it needs to actually render an HTML document (ie. it needs to render a page).
But there is a workaround to achieve it.
Redirect it to a page that renders out the title, . And from that page trigger the browser over to the download.
Try this !!!!
In html/jsp page:
<a href="pdf/filename.pdf">
In servlet:
String filename = request.getPathInfo().substring(1);//filename.pdf
Also you cen remove .pdf from title by simple java operation.
content-disposition: attachment
Similar question found here
Replace your code with
response.setHeader("Content-disposition", "attachment;filename=" + title + ".pdf");
response.setContentType("application/octet-stream");
response.setContentLength(bArray.length);
try {
response.getOutputStream().write(bArray);
response.getOutputStream().close();}
Read answer description here
set metadata response
I have an issue with http response.
I send some data and in response I get attached pdf document, and I need to open it document in a new browser tab.
I can't do it using Window.Open(String url, String title, String... parameters) because I dont have this url.
if I open it with right click on and tell firebug "Open in a new Tab" I got an url
In order to display pdf in your page you should have Content-Type: application/pdf in your response header. So, You should set in response header in your servlet which is responsible that given url.
After that you can put your pdf inside one of these html tags embed, object or iframe. I think gwt's Frame is a good way to do it. See this tutorial as well: 4 Ways To Stream Pdf and Some Tips
I need to open a pdf file for the user who clicks on a download button.
The download button makes a ajax call to a servlet which gets data from a blob field in the database containing PDF contents, and returns it as a response.
What can I do in order to make the response to be downloaded as a PDF file to the user.
The servlet code is as below:
response.setContentType("application/pdf");
oracle.sql.BLOB blob = (BLOB) rs.getBlob("MYPDF");
byte[] bytes = blob.getBytes(1, (int) blob.length());
ServletOutputStream servletOutputStream = response.getOutputStream();
servletOutputStream.write(bytes, 0, bytes.length);
servletOutputStream.flush();
servletOutputStream.close();
I receive a long response containing characters like the ones below in AJAX when I checked the Fire bug for the response.
�a�J��㔎��ji�2K���y�2��q F�f�9�G��!%�kɂ��W��������mp){h̕�S���NJ_�A����'����2k��j���яR�>wB�e�|=w�p�%w��qǦ>�~�1o�㾙9j�B�;aNx3�`z��طc�O��ï��$�;�N|;�xۇ��;�-�c�f�M��c���(�f�M6K���
I don't want to submit the page or to popup a window for the servlet with the parameters sent for the query showing in the URL
I also don't want to create the file on the server.
Is there a way to take the response of servlet coming in ajax call and display it to a page or Iframe and the browser automatically downloads the file...
Its is NOT possible to download a file using Ajax. However, the same 'effect' can be achieved using a hidden IFRAME. Instead of using XMLHttpRequest, dynamically create a hidden iframe and submit it to the servlet with the proper parameters. When the response comes, the browser will automatically handle the content based on the content-type/extension. If you are using jQuery, then this plugin has this functionality in-built.
Sounds like Rahulmohan knows his stuff but it might be worth trying to add a filename header to your response stream. I do something similar in asp.net but not in an Ajax environment and the only difference between my code and yours is this line:
Response.AddHeader("Content-Disposition", "attachment; filename=" & Filename & ".pdf")
My page doesn't run in an Ajax environment though.