Display PDF using an AJAX call

前端 未结 4 1721
广开言路
广开言路 2020-12-03 01:39

I\'m trying to display a PDF(which is created in the server side and pass to the client side as a web stream) through an AJAX call. My code is given below:

j         


        
4条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-03 02:15

    You can generate a temporary URL to access the PDF file stored on the server, instead of sending it back to the AJAX call. Just pass the generated URL back to the client. Then, when you receive the URL, you could for example do a window.location = to redirect the browser to the download.

    Make sure that the correct headers are set for the generated file (Content-Disposition: attachment etc.), and all should be fine.

    Edit: Although you could probably just use a regular (non-JavaScript) link to generate and download the file. But doing it via AJAX enables you to show some specific animation etc. to the user while the file is being generated.

提交回复
热议问题