java Firefox 不会将此文件下载为 CSV
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5156076/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Firefox will not download this file as a CSV
提问by Drew H
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.
我已经尝试了所有我能想到的。我已经改变了 100 次 mime 类型。更改标题 400 次。我已经看了十几次堆栈溢出。这在 Chrome 中运行良好。当我在 Firefox 中下载时,它认为它是一个 xlsx 文件或二进制文件。它甚至以 xlsx 格式打开,但它不认为它是 csv,因此列不会分开。如果我保存文件(而不是只是点击打开),它甚至不会打开扩展名。我什至还没有使用 IE,所以这有点让我担心。
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.
我试过 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.
Safari、Opera 和 Chrome 运行良好。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!
好的,这整个时间都是间距问题。我的文件名是“文件名.csv”,这适用于除 Firefox 之外的所有浏览器。当我把我的文件名放到“没有空格的文件名.csv,它下载它找到了。我没有注意到下载时它只下载了空格之前的名字的第一部分。祝你好运!
采纳答案by Drew H
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.
好的,这整个时间都是间距问题。我的文件名是“文件名.csv”,这适用于除 Firefox 之外的所有浏览器。当我将我的文件名放入“没有空格的文件名.csv,它下载它找到。我没有注意到下载时它只下载了空格之前的名称的第一部分。
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.
将来确保文件名在标题中有一个单引号。如果您需要一个空格作为文件名,这将允许 Firefox 正确下载它(不会去除空格之外的任何内容)。
Good luck!
祝你好运!
回答by Oharrington
I had the same issue in PHP and found adding double quotes for the file name fixes the problem.
我在 PHP 中遇到了同样的问题,发现为文件名添加双引号可以解决这个问题。
response.setHeader("Content-disposition", "attachment; filename=\"" + fileName + \"");
回答by Arne Burmeister
The content type text/csv is correct, but you should also add an charset encoding:
内容类型 text/csv 是正确的,但您还应该添加字符集编码:
response.setHeader("Content-type: text/csv; charset=utf-8");
But what the hell is this:
但这是什么鬼:
response.setHeader("Content-Transfer-Encoding", "binary");
response.setHeader("Content-length", String.valueOf(stringList.size()));
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!
删除那个标题!内容长度以字节为单位。不要试图自己计算。在这个例子中绝对是错误的!具有主要类型的 Mime-Typetext
不是二进制的!
回答by emeraldjava
Add the content-type header with value text/csv
添加值为 text/csv 的内容类型标题
response.setHeader("Content-type: text/x-csv");
回答by user5375275
Response.AddHeader("content-disposition", string.Format("attachment;filename=\"{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";
回答by Kumar
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.
我不是 Java/JSP 专家,但你确定这对 text/csv 是正确的吗?
response.setHeader("Content-Transfer-Encoding", "binary");
你有没有尝试注释掉这个?在 PHP 中,我将简单地回显/打印带有标题内容类型标题和处置类型的 CSV。