" to the downloaded CSV file

My code works for IE and FF. In Chrome, the browser appends "-" hyphen to the start and end of the file name, thus making it unable to recognize the file type. On renaming the file while saving as csv makes it open in Excel in a single cell but I want a solution to handle it in the code side. It seems difficult.

Below is my code:

//fileName = xxxx.csv
response.setContentType("application/download");
response.setHeader("Cache-Control", "public"); 
response.setHeader("Content-Type", "application/octet-stream");
response.setHeader("Content-Disposition", "attachment; filename= "" + fileName + """);

Note: I searched many blogs but didn't find the solution for Chrome.


I'm seeing two things:

  • You shouldn't have a space after filename= in your Content-Disposition .
  • I've never used quotes around the filename; I don't think you're meant to. I don't see anything about using quotes in the RFC, so I'd get rid of them.
  • I expect you're seeing a hyphen because Chrome is replacing either the space or (more likely) the quotes with hyphens because it considers the quotes invalid characters for the filename on your OS.

    FWIW, my working Content-Disposition headers look like this:

    Content-Disposition: attachment;filename=someFileName.csv

    Off-topic : Re your statement:

    My code works for IE and FF. In Chrome, the browser appends "-" hypen to the start and end of the file name, thus making it unable to recognize the file type.

    Browsers should (and mostly do) recognise file type by the MIME type, not the file extension. You might want to set Content-Type to text/csv rather than application/octet-stream . (Of course, if you're talking about what the OS does with the file later, that's another thing.)


    It's a bug/feature of Chrome. https://code.google.com/p/chromium/issues/detail?id=69939

    Looks like Chrome considers the quotes part of the filename but realizes that they are valid characters for a filename and so converts them to hyphens.

    RFC2183 defines the Content-Disposition header but does not say what should happen if the server encloses the filename in quotes.

    http://www.ietf.org/rfc/rfc2183.txt (from 1st comment)

    You must remove quotes from filename part of the Content-Disposition. I'm not sure what happens with blanks in filenames without quotes. That needs to be tested.


    Although invalid characters can make this occur, this trailing hyphen filename issue only happens for me in different circumstances than those listed by the answer answers.

    Long yesterday = new Date().getTime() - (1000*60*60*24);
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    
    String theCsv = "stuff,stuffn yeah, uhuhn";
    
    //This sends a normal file
    Attachment att = new Attachment("myfile.csv", theCsv.getBytes(), "UTF-8");
    sendEmailWithAttachment("you@you.com","Me","me@me.com","Stuff for "+sdf.format(yesterday) ,"", att);
    
    //This sends one with a trailing hyphen!!
    String fileName = sdf.format(yesterday)+"-myfile.csv";
    fileName = fileName.replaceAll(" ", "").replaceAll(""", "");
    Attachment att = new Attachment( fileName, theCsv.getBytes(), "UTF-8");
    sendEmailWithAttachment("you@you.com", "Me","me@me.com","Stuff for "+sdf.format(yesterday),"", att);
    

    I can't explain this. The problem in my case isn't quotes or spaces--the only difference is the pre-parsed filename. Maybe try a filename without string concats?

    链接地址: http://www.djcxy.com/p/95090.html

    上一篇: Chrome将文件附件误解为文档

    下一篇: “到下载的CSV文件