简体   繁体   中英

Firefox and Chrome appending underscore before and after file name while Internet Explorer is working fine

Firefox and Chrome are appending underscores before and after the file name while Inte.net Explorer is working fine.

Firefox and Chrome give: _Warrant_Amendment_5485_14_March_2014.pdf.pdf_

IE gives: Warrant_Amendment_5485_14_March_2014.pdf.pdf

Below is the code

response.setContentType("application/pdf");
response.setHeader("Content-Disposition", "inline;filename=\" + fileName + ".pdf\");

I resolved a similar issue by removing the quotes from the filename value (which in my case were not required).

I note that rfc6266 says a quoted-string for the filename value should be acceptable. At this point I have not investigated further.

I've just faced the same problem and solved it thanks to user650881's reply.

The problem was I had this:

response.addHeader("Content-Disposition","attachment; filename=" + filename + "\"");

And worked when I changed it to this:

response.addHeader("Content-Disposition","attachment; filename=\"" + filename + "\"");

Note the \\"" after filename=

Hope it helps

This happens when there are characters that are invalid for a file name.
C#'s Path.GetInvalidFileNameChars method lists most.

Round, curly and square brackets may be legal in a file name (it is on Windows), but files with them will also get the underscores added by browsers.

I've got the same issue and I thought that user650881 's answer is the solution, but in my case problem was I was parsing the file name from Content-Disposition wrongly, that is I kept the last quote " char from attachment; filename="filename.ext" string in the filename ( filename.ext" ) which was automatically transformed to _ when downloading the file like this:

  const link = document.createElement('a');

  link.href = window.URL.createObjectURL(blob);
  // Contains 'filename.ext"' string which is replaced by 'filename.ext_`
  link.download = fileName; 
  document.body.appendChild(link);
  link.click();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM