简体   繁体   English

Firefox不会将该文件下载为CSV文件

[英]Firefox will not download this file as a CSV

I have tried everything I can think of. 我已经尝试了所有我能想到的。 I have changed the mime type 100 times. 我已将mime类型更改了100次。 Changed the headers 400 times. 更改标题为400次。 I've looked through stack over flow a dozen times. 我已经遍历了栈十多次。 This works fine in Chrome. 在Chrome中可以正常使用。 Soon as I go to download in Firefox it thinks it's a xlsx file, or a binary file. 当我在Firefox中下载文件时,很快就认为它是xlsx文件或二进制文件。 It even opens as an xlsx but it doesn't think it's a csv so the columns aren't seperated. 它甚至以xlsx的形式打开,但它不认为这是csv,因此各列之间没有分隔。 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. 我什至还没有去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. Safari,Opera和Chrome浏览器都可以正常工作。 Haven't tried IE. 还没有尝试过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. 我的文件名为“ file name.csv”,它在除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! 不久,当我将文件名输入“ filename.csv”时,它没有下载任何空格。我没有注意到,当下载文件时,它只是下载了该名称的第一部分,而不是空格。祝您好运!

我在PHP中遇到了同样的问题,发现在文件名中添加双引号可以解决该问题。

 response.setHeader("Content-disposition", "attachment; filename=\"" + fileName + \"");

Ok this entire time it was a spacing issue. 好的,这一直是间距问题。 My file name was "file name.csv" and this works in every browser except firefox. 我的文件名为“ file name.csv”,它在除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. 不久,当我将文件名输入“ filename.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! 祝好运!

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! 具有主要类型text Mime-Type不是二进制的!

Response.AddHeader("content-disposition", string.Format("attachment;filename=\\"{0}\\"", fileName)); Response.AddHeader(“ content-disposition”,string.Format(“ attachment; filename = \\” {0} \\“”,fileName))); Response.ContentType = "text/csv; charset=utf-8"; Response.ContentType =“ text / csv; charset = utf-8”;

添加具有text / csv值的content-type标头

response.setHeader("Content-type: text/x-csv");

I am not expert in Java/JSP but are you sure this is correct for text/csv? 我不是Java / JSP专家,但是您确定这对text / csv是正确的吗?
response.setHeader("Content-Transfer-Encoding", "binary"); 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. 在PHP中,我将简单地对CSV进行回显/打印,并在其前面加上标题content-type标题和处置类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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