How do I force files to open in the browser instead of downloading (PDF)?

后端 未结 13 1126
别跟我提以往
别跟我提以往 2020-11-22 04:15

Is there a way to force PDF files to open in the browser when the option \"Display PDF in browser\" is unchecked?

I tried using the embed tag and an iframe, but it o

13条回答
  •  遥遥无期
    2020-11-22 04:52

    The correct type is application/pdf for PDF, not application/force-download. This looks like a hack for some legacy browsers. Always use the correct mimetype if you can.

    If you have control over the server code:

    • Forced download/prompt: use header("Content-Disposition", "attachment; filename=myfilename.myextension");
    • Browser tries to open it: use header("Content-Disposition", "inline; filename=myfilename.myextension");

    No control over the server code:

    • Use the HTML5 download attribute. It uses the custom filename specified on the view side.

    NOTE: I prefer setting the filename on the server side as you may have more information and can use common code.

提交回复
热议问题