Open/Save as… dialog box not showing

2019-08-15 03:54发布

I have a PDF file on my server that I need the user to download from the client side.

Using Spring Framework, I use javax.servlet.http.HttpServletResponse to create the correct response and corresponding header:

response.setHeader("Expires", "-1");
response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
response.setHeader("Pragma", "public");
response.setContentType("application/pdf");
response.setHeader("Content-Disposition", "attachment;filename="content.pdf");
response.setContentLength(content.size());

Then I use the ServletOutputStream to write the content:

ServletOutputStream os;
try {
    os = response.getOutputStream();
    os.write(((ByteArrayOutputStream)baos).toByteArray());
    baos.close();
    os.flush();
    os.close();
} catch (IOException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
}

On the client side, I receive HTTP code 200 and receive the correct response body, with the PDF file, but the 'Save As...' pop-up did not appear.

Is there any reason on the Header configuration that could lead to this problem or could it be somewhere else?

Thank you.

3条回答
Lonely孤独者°
2楼-- · 2019-08-15 04:00

Try setting the content type to application/octet-stream instead:

response.setContentType("application/octet-stream");

This should force the browser to show the "Save As..." popup. If you set it to application/pdf the browser recognizes the file type and displays it instead.

查看更多
干净又极端
3楼-- · 2019-08-15 04:12

maybe:

attachment;spacefilename=content.pdf

update

public static void download(HttpServletResponse response, File file, String downloadName) throws IOException
{
    if(file == null) throw new IllegalArgumentException("file is null");

    response.reset();
    response.setHeader("Content-Length", String.valueOf(file.length()));
    response.setContentType(new MimetypesFileTypeMap().getContentType(file));
    response.setHeader("Content-Disposition", "attachment; filename=\"" + downloadName + "\"");

    InputStream input = new FileInputStream(file);

    try
    {
        OutputStream output = response.getOutputStream();

        try
        {
            IOUtils.copy(input, output);
        }
        catch(IOException e)
        {
            e.printStackTrace();

            throw e;
        }
        finally
        {
            output.close();
        }
    }
    catch(IOException e)
    {
        throw e;
    }
    finally
    {
        input.close();
    }
}

and the only difference i see is in the headers section. have you tried without cache-control, pragma and expires?

update

no difference at all using a file or a stream:

public static void download(HttpServletResponse response, InputStream input, String downloadName, String contenType) throws IOException
{
    response.reset();
    response.setHeader("Content-Length", String.valueOf(input.available()));
    response.setContentType(contenType);
    response.setHeader("Content-Disposition", "attachment; filename=\"" + downloadName + "\"");

    OutputStream output = response.getOutputStream();
    IOUtils.copy(input, output);
    input.close();
}
查看更多
我命由我不由天
4楼-- · 2019-08-15 04:12

When I ran this code the problem came in

response.setHeader("Content-Disposition", "attachment;filename="content.pdf");

for defining filename

Try:

response.setHeader("Content-Disposition", "attachment;filename="+"content.pdf");

It will open up dialog box and will give you save as option when clicking save button

查看更多
登录 后发表回答