Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firefox will not download this file as a CSV

Tags:

java

csv

servlets

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!

like image 325
Drew H Avatar asked Mar 01 '11 14:03

Drew H


1 Answers

I had the same issue in PHP and found adding double quotes for the file name fixes the problem.

 response.setHeader("Content-disposition", "attachment; filename=\"" + fileName + \"");
like image 61
Oharrington Avatar answered Oct 13 '22 01:10

Oharrington