Save as Content-Disposition filename in Explore 11 - java

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 + "\"");

Related

Set the exact file name while downloading the files

I’m trying to download a file and, dynamically, I’m setting the file name. But while downloading the file I’m receiving the file name as "3823720b-3d63-4e2c-8fc2-6abdc34b5cd8.docx" But I’m passing my file name as "Sample.docx"
if (Files.exists(file)) {
response.setContentType(existingDocuments.getContentType());
response.addHeader("Content-Disposition", "attachment; filename=" + existingDocuments.getFileName());
Files.copy(file, response.getOutputStream());
response.getOutputStream().flush();
}
this is working for me
response.header("content-disposition", "attachment; filename=\"" + fileNameDisplay + "\"");
you might also need to add
response.setContentType("application/octet-stream");

File can not be copied to response steam

There is an interesting error on a page. I wonder if you have any idea about it.
In order to download a file, we write the file to responseStream but if file is less than 8,3KB, we get file not found error although file is there.
If i increase the file size (using some dummy values), it works
I suspect it has something to do with the contentLength of response but it seems fine to me
file = new File(ef.getPath());
response.setContentLength((int) file.length());
fileIs = new FileInputStream(file);
FileCopyUtils.copy(fileIs, response.getOutputStream());
Please help me if you have any idea?
How try with adding Content-Disposition header and setContentType()
response.setHeader("Content-Disposition", "attachment; filename=\""+ fileName + "\"");
String mimeType = new MimetypesFileTypeMap().getContentType(fileName);
response.setContentType(mimetype);

Content-Disposition file extension not properly working on Firefox

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.

Complete file name does not appear while downloading via servlet

I have a servlet where I have written code to download a file.
The part of code is as follows :
response.setContentType((mimetype != null) ? mimetype
: "application/octet-stream");
response.setHeader("Content-Disposition", "attachment; filename="
+ fileName);
OutputStream os = response.getOutputStream();
try {
//here getFileByte is a method for getting
byte bytes[] = getFileByte(filePath);
os.write(bytes);
}
The above works fine but the only problem is that when the filename contains more than one word then it downloads the file with the first word.
For example:- Filename is "Step by Step.pdf" then the downloaded file
will be "Step.pdf".
I even tried to print the filename before giving it to setHeader method and it was correct. Don't have any clue how to resolve this.Could anyone please check what am I doing wrong here and how to correct this bug?
If your file has spaces in it, it should be enclosed in double quotes:
Content-disposition: attachement; filename="file with spaces.whatever"
Note that double quotes will work equally well for filenames without spaces, so you might as well use them all the time
Have you try to encode your filename ? For exemple, replacing spaces by "%20" character sequence ?

Question regarding streams in java

We have the below requirement.
We will have to create an excel/pdf report and then download it on click of a button in a java web application. The pdf/excel file is dynamically created using application data.
We should not create any physical file on the server.
How do we go about this? Are there any streams through which I can read and write in the same go without having to close in between.
You could use memory-based streams (such as ByteArrayInputStream and ByteArrayOutputStream) and use the same underlying byte buffer to address the read/write in the same go part of the question.
As others have pointed out, you can just write directly to the output stream of the response.
Look at ServletResponse.getOutputStream().
You need to write to this stream from the one created by your report API. Don't forget to set the proper content-type using setContentType() method of the same class.
Here you can find how you can do it with jxl API and it may help you also.
How do I output an Excel file from a Servlet?
Whatever PDF or Excel API you are using to generate the files, you should lookup the constructor or method which takes an OutputStream to write the generated PDF/Excel content to. You should just feed it with response.getOutputStream() instead of FileOutputStream.
For example, iText for PDFs:
response.setContentType("application/pdf");
response.setHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");
PdfWriter pdfWriter = PdfWriter.getInstance(document, response.getOutputStream());
// ...
And Apache POI for Excel:
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");
WritableWorkbook workBook = Workbook.createWorkbook(response.getOutputStream());
// ...
Have a Servlet serve the pdf/excel file as a byte array.
#Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
byte[] bytes = null; // get this from somewhere in your app
String fileName = "filename.pdf"; // whatever you wish to name the file
ServletOutputStream out = response.getOutputStream();
response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
response.setContentType("application/pdf");
response.setContentLength(bytes.length);
out.write(bytes);
out.flush();
}
MIME type for MS Excel files would be application/vnd.ms-excel.

Categories

Resources