download file name

yac678 used Ask the Experts™
Is it possible to control the file name when it is downloaded by Chrome?

I have a servlet for performing a file download. The servlet name is DownloadResearch. The download occures in the background using AJAX and an IFrame as follows:

    function downloadResearchUsingIFrame() {
            if (elemIF == "") {
                elemIF = document.createElement("iframe");
       = "none";
            var url = "DownloadResearch?"+getResearchIndexForQueryString();
            elemIF.src = url;
The code in the servlet is pretty straight forward:
String contentType = getServletContext().getMimeType(exportFile.getName());
ServletOutputStream out = response.getOutputStream();
ServletUtils.returnFile(exportFile.getAbsolutePath(), out);
public static void returnFile(String filename, OutputStream out)
            throws FileNotFoundException, IOException {
        // A FileInputStream is for bytes
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(filename);
            byte[] buf = new byte[4 * 1024];  // 4K buffer
            int bytesRead;
            while ((bytesRead = != -1) {
                out.write(buf, 0, bytesRead);
        finally {
            if (fis != null) fis.close();
The problem is that when the file arrives at the browser, Chrome does not open a file browse dialog box but rather writes the file to C:\Documents and Settings\Administrator\My Documents\Downloads with a filename that equals the servlet name: DownloadResearch.xls (because I set the mime type to "xls").
Is there a way to control the file name?
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®


Wow, this worked!
One small problem stil remains. The file name is in Hebrew and it contains spaces. When I convert it to URL format using the file name that is created contains "+" signs instead of the spaces. I tried to convert all "+" signs to "%20" but that did not work. Is there any way to obtain a file name with spaces in the client?
Not sure if it is possible, generally file names/urls with spaces are not accessible over internet
HTML5 and CSS3 Fundamentals

Build a website from the ground up by first learning the fundamentals of HTML5 and CSS3, the two popular programming languages used to present content online. HTML deals with fonts, colors, graphics, and hyperlinks, while CSS describes how HTML elements are to be displayed.

replace space with underscore or other ascii character, since urls/filenames with non-ascii character are will be escaped anyways, like space is escaped with %20 in this case


There must be a way. When a file arrives via gmail as an attachment and has spaces in the file name, it is downloaded with no problem to the file system. A single click in Chrome and the file is downloaded to C:\Documents and Settings\Administrator\My Documents\Downloads and the spaces appear in the file name. Any idea how Google does the trick?
Most Valuable Expert 2011
Top Expert 2016

Try making the spaces into plus signs like+this and see if that helps.  In PHP this is the urlecode() function.  Not directly applicable but the principles should be the same.  HTH, ~Ray

Place the filename in quotation marks.

response.setHeader("Content-disposition", "attachment;filename='the quick brown fox..xm'l");


Adding the quotation mark did not help. I simply got the file name with the qutation marks in it. I did manage to solve the problem however.
Please note that I encode the file name because it contains Hebrew characters. After encoding using URLEncoder.encode(fileName, "UTF-8") I get a file name such as:

In my previous reply 33128793 I said "I tried to convert all "+" signs to "%20" but that did not work". I assume I did not convert properly. I retried this and after trying  

            String encodedFileName = URLEncoder.encode(fileName, "UTF-8");
            encodedFileName = encodedFileName.replaceAll("\\+", "%20");
            response.setHeader("Content-disposition", "attachment;filename="+encodedFileName);

I got the proper file name with Hebrew characters and spaces!


I solved one last problem on my own: a problem with the file name containing spaces.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial