Force browser to open file instead of prompting download

前端 未结 3 2556
孤独总比滥情好
孤独总比滥情好 2021-02-20 18:49

When clicking a PDF link in Firefox and Chrome, the file will sometimes be opened for in-browser viewing and sometimes prompt a \"Save as\" dialog.

If I wanted to force

3条回答
  •  萌比男神i
    2021-02-20 19:40

    You can achieve that by setting the appropriate header (for instance, in case of PDF, the header will be Content-type: application/pdf;

    With this header, the browser will know the mime-type of the file and display it if it is compatible with it.

    Here you can see the headers for a PDF.
    As a hint, what I like to do is to use some sort of controller (in case you are using a backend language) that handles the download. Hence, to download myNewProject.pdf I do

    Download!
    

    Then I can set the appropriate headers depending on the file type, or if I want to force download or view it in the browser...

提交回复
热议问题