Firefox 不会将此文件下载为 CSV
我已经尝试了我能想到的一切。我已经改变了 mime 类型 100 次。更改标题 400 次。我已经查看了堆栈溢出流十几次。这在 Chrome 中运行良好。当我在 Firefox 中下载时,它认为这是一个 xlsx 文件或二进制文件。它甚至以 xlsx 格式打开,但它不认为它是 csv,因此列没有分开。如果我保存文件(而不是直接打开),它甚至不会添加扩展名。我什至还没有接触过 IE,所以这让我有点担心。
mime mapping
<mime-mapping>
<extension>csv</extension>
<mime-type>application/vnd.ms-excel</mime-type>
</mime-mapping>
我尝试过文本/csv、应用程序/csv、应用程序/二进制、应用程序/八位字节流。
public void doDownloadFile() {
PrintWriter out = null;
try {
String fileName = selectedPkgLine.getShortname() + ".csv";
HttpServletResponse response = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();
HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
response.setHeader("Pragma", "public");
response.setHeader("Expires", "0");
response.setContentType(request.getServletContext().getMimeType(fileName));
response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
response.setHeader("Content-disposition", "attachment; filename=" + fileName + "");
response.setHeader("Content-Transfer-Encoding", "binary");
out = response.getWriter();
CSVWriter writer = new CSVWriter(out);
List<PkgLoad> pkgLoadList = pkgLoadService.findBetweenDates(selectedPkgLine, startDate, endDate);
List<String[]> stringList = new ArrayList<String[]>();
stringList.clear();
String[] header = {
"pkg_load_id",
"time_stamp",
"ounces",
"revolutions",
"wrap_spec_id",
"pkg_line_id"
};
stringList.add(header);
for (PkgLoad pkgLoad : pkgLoadList) {
String[] string = {
pkgLoad.getPkgLoadId().toString(),
pkgLoad.getTimeStamp().toString(),
pkgLoad.getOunces().toString(),
pkgLoad.getRevolutions().toString(),
pkgLoad.getWrapSpecId().getWrapSpecId().toString(),
pkgLoad.getPkgLineId().getPkgLineId().toString()
};
stringList.add(string);
}
response.setHeader("Content-length", String.valueOf(stringList.size()));
writer.writeAll(stringList);
out.flush();
} catch (IOException ex) {
Logger.getLogger(ViewLines.class.getName()).log(Level.SEVERE, null, ex);
} finally {
out.close();
}
}
感谢您的任何帮助。
Safari、Opera 和 Chrome 运行良好。没试过IE。
****编辑****
好吧,这一直是一个间距问题。我的文件名是“文件名.csv”,这适用于除 Firefox 之外的所有浏览器。当我将文件名设置为“filename.csv”时,它会下载并找到没有空格的文件名。我没有注意到,当它下载时,它只下载了空格之前的名称的第一部分。祝你好运!
I have tried everything I can think of. I have changed the mime type 100 times. Changed the headers 400 times. I've looked through stack over flow a dozen times. This works fine in Chrome. Soon as I go to download in Firefox it thinks it's a xlsx file, or a binary file. It even opens as an xlsx but it doesn't think it's a csv so the columns aren't seperated. If I save the file(instead of just hit open) it doesn't even put the extension on. I haven't even got to IE yet so this is kind of worrying me.
mime mapping
<mime-mapping>
<extension>csv</extension>
<mime-type>application/vnd.ms-excel</mime-type>
</mime-mapping>
I've tried text/csv, application/csv, application/binary, application/octet-stream.
public void doDownloadFile() {
PrintWriter out = null;
try {
String fileName = selectedPkgLine.getShortname() + ".csv";
HttpServletResponse response = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();
HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
response.setHeader("Pragma", "public");
response.setHeader("Expires", "0");
response.setContentType(request.getServletContext().getMimeType(fileName));
response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
response.setHeader("Content-disposition", "attachment; filename=" + fileName + "");
response.setHeader("Content-Transfer-Encoding", "binary");
out = response.getWriter();
CSVWriter writer = new CSVWriter(out);
List<PkgLoad> pkgLoadList = pkgLoadService.findBetweenDates(selectedPkgLine, startDate, endDate);
List<String[]> stringList = new ArrayList<String[]>();
stringList.clear();
String[] header = {
"pkg_load_id",
"time_stamp",
"ounces",
"revolutions",
"wrap_spec_id",
"pkg_line_id"
};
stringList.add(header);
for (PkgLoad pkgLoad : pkgLoadList) {
String[] string = {
pkgLoad.getPkgLoadId().toString(),
pkgLoad.getTimeStamp().toString(),
pkgLoad.getOunces().toString(),
pkgLoad.getRevolutions().toString(),
pkgLoad.getWrapSpecId().getWrapSpecId().toString(),
pkgLoad.getPkgLineId().getPkgLineId().toString()
};
stringList.add(string);
}
response.setHeader("Content-length", String.valueOf(stringList.size()));
writer.writeAll(stringList);
out.flush();
} catch (IOException ex) {
Logger.getLogger(ViewLines.class.getName()).log(Level.SEVERE, null, ex);
} finally {
out.close();
}
}
Thanks for any help.
Safari, Opera and Chrome work fine. Haven't tried IE.
****EDIT****
Ok this entire time it was a spacing issue. My file name was "file name.csv" and this works in every browser except firefox. Soon as I put my file name to "filename.csv with no spaces it downloaded it find. I didn't notice that when it was downloading it was only downloading the first part of the name before the space. Good luck!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我在 PHP 中遇到了同样的问题,发现为文件名添加双引号可以解决问题。
I had the same issue in PHP and found adding double quotes for the file name fixes the problem.
内容类型 text/csv 是正确的,但您还应该添加字符集编码:
但这到底是什么:
删除标头!内容长度以字节为单位。不要尝试自己计算。在这个例子中它绝对是错误的!主要类型为
text
的 Mime-Type 不是二进制的!The content type text/csv is correct, but you should also add an charset encoding:
But what the hell is this:
Remove that headers! The content length is in bytes. Do not try to calculate it by yourself. It is definitly wrong in this example! A Mime-Type with major type
text
is not binary!好吧,这一直是一个间距问题。我的文件名是“文件名.csv”,这适用于除 Firefox 之外的所有浏览器。当我将我的文件名放入“filename.csv”时,它会下载它,它没有空格。我没有注意到,当它下载时,它只下载空格之前的名称的第一部分。
将来确保 需要文件名中的空格,这将允许 Firefox 正确下载它(不会删除空格后面的任何内容)。
文件名在标题中带有单引号。如果您
Ok this entire time it was a spacing issue. My file name was "file name.csv" and this works in every browser except firefox. Soon as I put my file name to "filename.csv with no spaces it downloaded it find. I didn't notice that when it was downloading it was only downloading the first part of the name before the space.
In the future make sure the filename has a single quote around it in the header. This will allow Firefox to download it correctly(without stripping off anything past the space) if you need a space the file name.
Good luck!
添加带有值 text/csv 的内容类型标题
Add the content-type header with value text/csv
Response.AddHeader("内容处置", string.Format("附件;文件名=\"{0}\"", fileName));
Response.ContentType = "text/csv; charset=utf-8";
Response.AddHeader("content-disposition", string.Format("attachment;filename=\"{0}\"", fileName));
Response.ContentType = "text/csv; charset=utf-8";
我不是 Java/JSP 方面的专家,但您确定这对于 text/csv 来说是正确的吗?
response.setHeader("内容传输编码", "二进制");
您尝试对此进行评论吗?在 PHP 中,我将简单地回显/打印 CSV,前面带有标题内容类型标题和处置类型。
I am not expert in Java/JSP but are you sure this is correct for text/csv?
response.setHeader("Content-Transfer-Encoding", "binary");
Did you try commenting out this? In PHP I will simply echo/print the CSV preceded with headers content-type headers and disposition type.