简体   繁体   中英

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. 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!

我在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. 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!

The content type text/csv is correct, but you should also add an charset encoding:

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!

Response.AddHeader("content-disposition", string.Format("attachment;filename=\\"{0}\\"", fileName)); 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?
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.

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