I have the following code on Spring Controller
#RequestMapping(value = "/download", method = RequestMethod.POST)
public void downloadActive(#RequestParam(value = "type") String offerType, HttpServletResponse response,
HttpSession session) throws Exception {
StringBuilder b = new StringBuilder();.
.
.
response.addHeader("Content-Disposition: ", "attachment; filename=my" + offerType + "Offers.csv");
response.getWriter().write(b.toString());
..
}
This code get executed when a download button from EXt Js is clicked. On chrome, it directly downloads the file as a *.csv format and when the user opens it, it will use Excel automatically to open the file, if they already have installed and this is the right behavior I wanted to have.
However, on firefox, it prompts a save as window with open with and save options. If I use open with option, it tells me the filename is filename.csv but the type is chrome htm file. I haven't mentioned htm anywhere in my code and I dont know why its trying to open it as htm instead of csv file. Once it tries to open it, it brings excel application and the contents are in a bad format.
Is there a way to avoid this problem on firefox ?
Thanks
Adding double quotes (") for the file name fixes the problem.
response.setHeader("Content-disposition", "attachment; filename=\"" + fileName + "");
Even if I am not sure why it worked, changing the addHeader Method to setHeader fixed the issue.
response.setHeader("Content-Disposition: ", "attachment; filename=my" + offerType + "Offers.csv");
PrintWriter printWriter = resourceResponse.getWriter();
java.util.Date date = new java.util.Date();
resourceResponse.setContentType("text/csv");
resourceResponse.addProperty(HttpHeaders.CACHE_CONTROL,
"max-age=0, must-revalidate");
resourceResponse.setProperty(
HttpHeaders.CONTENT_DISPOSITION,
"attachment; filename=" + "export_build" + "_"
+ date.getTime() + ".csv");
printWriter.write(stringBuilder.toString());
Make sure to write the content after printing the header to ensure content is packaged in a file properly.
Related
Internet Explorer 11 is ignoring my filename. When I try to "Save As" the archive name don't match with what I put on "filename=". Its getting from URL. Why others browsers works just fine? What do I have to do?
String szFilename = "archive.pdf";
response.setContentType("application/pdf");
response.setContentLength(pdfBytes.length - nBufferStart);
response.setHeader("Content-Disposition", "inline; filename=" + szFilename);
String szFilename = "archive.pdf";
response.setContentType("application/pdf");
response.setContentLength(pdfBytes.length - nBufferStart);
response.setHeader("Content-Disposition", "inline; filename=" + szFilename);
I have created a sample using the similar code, it seems that your code works well in IE 11. Please try to put the file name in double quotes, like this(it also works well in IE11 browser):
response.setHeader("Content-Disposition", "inline; filename=\"" + szFilename + "\"");
I am outputting a PDF file in a Web browser (IE8) HttpContext.Response.writefile(fileName) and it works great. When I try to save the file, it will give me the name of the ashx handler as a default. I would like to actually pass the real name.
I tried to add header information as follow:
context.Response.AddHeader("content-disposition", "attachment; filename=" + fileInfo.Name);
And it works but I do not want the user to have to choose between open and save, i want the file to open normally and if the user chooses to save it then the dialog would give him/her the default filename.
I tried also:
context.Response.AddHeader("content-disposition", "inline; filename=" + fileInfo.Name);
Or just, like Scott Hanselman suggested in his blog.
context.Response.AddHeader("content-disposition", "filename=" + fileInfo.Name);
None of those work for me. Does anybody have any ideas?
See test cases at http://greenbytes.de/tech/tc2231/#inlwithasciifilenamepdf - it seems that this is simply a missing feature in IE.
I also came across this problem.
What helped me was to also set the contenttype to application/pdf (instead of application/x-pdf, which is outdated)
response.setContentType("application/pdf");
response.setHeader("Content-disposition", "inline; filename=\"Report.pdf\"");
In case of INLINE, it seems that Internet explorer is using the last parameter in the URL to build the filename.
For example if your url is http://doc.com/131231231
IE will prompt you to save the file as 131231231.pdf
If you need a filename for example foo_131231231.pdf
you can hack the IE by using this url: http://doc.com/foo_131231231
You may suffer to change your app a bit to expect such ugly parameter, but at the end your app
will work as you expect.
So , I have some thing like this in the back end.
response.setContentType("text/csv; name=" + fileName);
response.addHeader("content-disposition", "attachment; filename=\"" + fileName + "\"");
try {
final String csvBuilder = this.reportService.tripSummaryCsvBuilder(trips);
response.getOutputStream().write(csvBuilder.getBytes());
} catch (Exception e) {
logger.error("downloadTripSummary() - Unexpected exception: ", e);
throw new ServerErrorException("download trip summary csv file failed");
}
So, All the back end code works fine. But the only problem here is I need to send like an ArrayList of 1000 objects to server , based on each element inside the arraylist the csv is built on the server.
If its a normal Http Get , it will works fine . But in this case, i need to use POST because I cant send 1000 objects as part of url.
I am using angular js on UI side . Could some one please assist me on how to do this ?
I have tried to use $http.post call using an ajax service in agular js, but the service is not able to pop up the window to save the file once the response is received.
http://plnkr.co/edit/N5bMq7dJ4YPJrppR1PPt
Check this plunker, with fileSaver u can save as fileName.
I found it here
I have a Java project which is used as a component in a webapp. This java code writes an xls file in a specific folder. I want to provide a download functionality for this file which should be triggered as soon as file writing is done.
The problem is - without a server environment, how can write a download functionality?
Don't write to file in a specific folder. Just write to the HTTP response body immediately. The downloading job should just be done in the webapp's code. I assume that you're using Servlets. If you set the HTTP response Content-Disposition header to attachment, then the browser will pop a Save as dialogue. If you also set the Content-Type header, then the browser will understand what to do with it (e.g. it will then be able to ask Do you want to open it in Excel or to save? and so on).
response.setHeader("Content-Type", "application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment;filename=\"" + filename + "\"");
// Now write xls to response.getOutputStream() instead of FileOutputStream.
If the API of that Java project is well designed, then you should have a method something like this:
public void writeXls(OutputStream output) throws IOException {
// Do your job to write xls to output. E.g. if you were using POI HSSF:
// WritableWorkbook workBook = Workbook.createWorkbook(output);
// ...
}
This way you can call it in the servlet as follows after setting the aforementioned headers:
yourClass.writeXls(response.getOutputStream());
Even more, it could easily be reused/tested in a plain vanilla Java application like follows:
yourClass.writeXls(new FileOutputStream("/path/to/foo.xls"));
This is how i do it. I show a download sql in my page.
response.setHeader("Content-Disposition", "attachment; " +
"filename=ContactPurge.sql");
response.setContentType("application/x-sql-data");
response.getWriter().write(procsql);
response.getWriter().write(sql);
response.flushBuffer();
I have run into an issue in which IE does not open up the Save As/Open dialog box for an Excel document like Firefox does.
So I have created a servlet filter that is using '*.xls' as the url pattern. The issue that I now face (since this is the first filter I have created) is how to get the name of the file that the user wants so that the dialog box gets populated correctly. Currently the filter is invoked when the user selects a link on a given page.
Here is what I came up with:
The above is what I have doFilter().
String fileName = "fileName.xls";
HttpServletRequest httpRequest = (HttpServletRequest) pRequest;
String requestURI = httpRequest.getRequestURI();
if(StringUtils.isNotBlank(requestURI))
{
String uri[] = StringUtils.split(requestURI, '/');
fileName = uri[uri.length - 1];
}
HttpServletResponse httpResponse = (HttpServletResponse) pResponse;
httpResponse.setContentType("application/vnd.ms-excel");
httpResponse.setHeader("Content-disposition", "attachment; filename=\"" + fileName +"\"");
web.xml:
<filter>
<filter-name>ExcelFilter</filter-name>
<filter-class>vsg.rp.common.ExcelFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>ExcelFilter</filter-name>
<url-pattern>*.xls</url-pattern>
</filter-mapping>
This all is working on my development box: Windows XP, JBoss, Eclipse, Oracle. But when it runs on the test server—Linux, Apache/JBoss, Oracle—it does not work. It appears that the filter is not even being called, no errors thrown, etc. Any idea as to why this would happen?
You want the content type set appropriately as well as the content disposition header, thus:
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition",
"attachment; filename=\"" + filename +
"\"");
Use the Content-Disposition HTTP header and set it to something like:
attachment; filename=myworkbook.xls
The is a Microsoft Knowledge Base Article all about this problem. And here is an example of setting Content-Disposition in Java code.
In addition to setting the headers for the content type, you'll also want to ensure that the server DOES NOT tell the browser to NOT CACHE the file.
In IE land, if you tell IE not to cache the file, it happily downloads the file... then tries to open the file from the directory it saved the file to... However since the headers said "don't cache" it takes this literally, and doesn't save the file, thus Excel throws an error saying, "There is no file!".
Long story short, tell IE to CACHE the file.