I have text files stored in GAE Blobstore.
Now I want to read the contents of the file and view them in my browser, without actually downloading the file.
I am accessing the servlet by clicking on link on my HTML page which access the servlet.
I am serving the file with blobKey
String blobKey = req.getParameter("blobKey");
BlobKey blobKey = new BlobKey(blobKey);
resp.setContentType("text/plain");
resp.setHeader("Content-Disposition", "attachment; filename=\"" +fileName +"\"");
blobstoreService.serve(blobKey, resp);
But the above code is downloading the file instead by displaying it in a browser.
Can anyone help me please.
Remove the line setting the Content-Disposition in the header, that should do the trick.
Related
I'm trying to open a PDF file by requesting it from Rest endpoint. But in this case, the file is getting downloaded. I tried opening it in another window. There also it just shows new window and downloads it there.
I used Chrome. So in Chrome by default it downloads the file while in Firefox it shows the dialog box whether to open it or save it. I don't want that dialog box. But want to display the file in new window with all the features like download, print, etc which a normal pdf viewer will show.
Is there some way through which I can avoid downloading of the file by default and just display that file in another window? Content-Disposition is attachment; filename="abc.pdf" when I see the properties of the URL. Also, its content-type is application/pdf;charset=utf-8.
<a target="_blank" data-content-type="application/pdf" onclick="open(this.href, this.target, 'fullscreen=yes'); return false;" data-type="downloadTenPointDocument" href="<c:out value="${resultItem.offer.programInfoUrl}"/>">View 10-point</a>
I changed the properties of pdf file from attachment to inline and set it in response:
response.setContentType("application/pdf");
response.addHeader("Content-Disposition", "inline; filename=" + fileName + ".pdf");
I am requesting the pdf file from server and then reading it and setting it in response, changing the properties of it in response, so that its default behavior of getting downloaded is overwritten.
Thank you everyone. The answer to change the property of pdf file was right.
You won't be able to do it directly from REST endpoint.
Use target="_blank" in anchor element on your page.
go
You should try to erase attachment; property from content disposition header.
i did like that and it worked: (it wont start to download, and it will open in a new tab (target _blank) if same tab, thenit is (target _self)
show pdf in a new tab
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 am exporting to JSP page using jasperexportmanager as below.
jasperPrint=JasperFillManager.fillReport(statReportSummaryJasper, param,jre);
ServletOutputStream servletOutputStream = res.getOutputStream();
res.setContentType("application/PDF");
JasperExportManager.exportReportToPdfStream(jasperPrint, servletOutputStream);
servletOutputStream.flush();
servletOutputStream.close();
This gets rendered in JSP page. When I try to download, It gives me option to save as html and not as PDF. How do I save this as PDF.
Any help or direction will be of great help. Thank you.
#Nagarajan....
add this code and try once again...
response.setHeader("Content-Disposition", "attachment; filename="
+ ReportName + ".pdf");
I just changed the code as below response.setHeader("Content-Disposition", "inline; filename=" + ReportName + ".pdf"); And it opened the PDF in the browser and the user can save the PDF if he wants. Thank you all again. Objective: Accomplised
This is a "non-programmers" answer!
Our bank generates statements in jsp which I wanted to save as pdfs (in order to email them to a person even less technical than I am). In the end, I just opened the downloaded jsp in my browser (Chrome) and printed it to a pdf. That was satisfactory for me.
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();
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.