user2574678
user2574678

Reputation: 709

EXTJS 4.2 - download PDF via REST API

I am new to EXTJS4.2 and web development, learning but need some insight into an area that I am confused by.

My AJAX code is calling a POST REST API via JSON input params that will download a PDF file.

During my research, some users have indicated that it is not possible to use AJAX to download the PDF, other posts suggest using AJAX with submit form params to download the PDF via IFRAME,

So, I am a little confused as I do not understand what I need to do, I have tried a few things with no luck.

Could someone provide some background on what is possible and what is not and how I actually achieve this, can I not write the responseText to file?

I am looking to use a solution that will work across all browser, hence let EXTJS do the lifting.

Not sure how much this helps, but this is what the response content type looks like

Transfer-Encoding: chunked 
Content-Type: application/pdf;charset=utf-8
Content-Disposition: attachment;filename=1428140714.pdf

Upvotes: 1

Views: 1551

Answers (1)

Saki
Saki

Reputation: 5856

Although you could download the file via Ajax, there is no way how to trigger the browser save dialog so the downloaded file would stay in the memory only.

Fortunately, the solution is easy: You post a request from the client with the target being a hidden iframe. The browser save dialog appears, user can select a location for save and the file is saved. You can delete the iframe afterwards.

You can see it in action here: http://extjs.eu/ext-examples/#downloading-file

Upvotes: 4

Related Questions