Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

Professional Opinions
Ask a Question
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

troubleshooting Question

download file name

Avatar of yac678
yac678 asked on
Web DevelopmentWeb BrowsersJava EE
9 Comments1 Solution1566 ViewsLast Modified:
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");
                elemIF.style.display = "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 = fis.read(buf)) != -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?
Avatar of Gurvinder Pal Singh
Gurvinder Pal SinghFlag of India image

Our community of experts have been thoroughly vetted for their expertise and industry experience.

This problem has been solved!
Unlock 1 Answer and 9 Comments.
See Answers