Firefox不会将此文件作为CSV下载 [英] Firefox will not download this file as a CSV

查看:518
本文介绍了Firefox不会将此文件作为CSV下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试过了我能想到的一切。我改变了mime类型100次。更改标题400次。我通过堆栈查看十几次。这在Chrome中工作正常。很快,当我去在Firefox下载它认为它是一个xlsx文件或二进制文件。它甚至打开为xlsx,但它不认为它是一个csv,所以列不分离。如果我保存文件(而不是只是打开),它甚至不把扩展名。我甚至没有到IE,所以这是有点担心我。

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> 

我尝试过text / csv,application / csv,application / binary,application / octet-stream 。

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();
            }
        }

感谢任何帮助。

Safari,Opera和Chrome可以正常工作。尚未尝试过IE。

Safari, Opera and Chrome work fine. Haven't tried IE.

是间距问题。我的文件名是file name.csv,这个工作在每个浏览器,除了firefox。很快,我把我的文件名称为filename.csv没有空间下载它找到我没有注意到,当它下载它只下载的名字的第一部分之前的空间。祝你好运!

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!

推荐答案

确定这是一个间距问题我的文件名是file name.csv 。很快,我把我的文件名称filename.csv没有空间下载它找到。我没有注意到,当它正在下载它只下载的空间之前的名称的第一部分。

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.

祝你好运!

这篇关于Firefox不会将此文件作为CSV下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆