The more interesting thing about this is how the file is downloaded after sending the HTTP request. Let's dive straight into it:. The actual download is done by creating a Blob object, which is used for a newly created a tag with a link to the created Blob object which is automatically clicked which ultimately opens the "Save file" dialog.
Additionally it's appended to the body which is a fix for Firefox and is removed from the body afterwards we don't want to have tons of invisible a tags on our body. The example below is simplified. A simple event on button click. It creates an AjaxDownloadFile object.
The AjaxDownloadFile class source is below. This worked for me. Found this plugin here. What worked for me is the following code, as the server function is retrieving File memoryStream.
If you have to work with file-stream so no physically saved PDF like we do and you want to download the PDF without page-reload, the following function works for us:.
A simple event on button click. It creates an AjaxDownloadFile object. The AjaxDownloadFile class source is below. What worked for me is the following code, as the server function is retrieving File memoryStream. This worked for me. Found this plugin here.
Concerning the answer given by Mayur Padshala this is the correct logic to download a pdf file via ajax but as others report in the comments this solution is indeed downloads a blank pdf. The reason for this is explained in the accepted answer of this question : jQuery has some issues loading binary data using AJAX requests, as it does not yet implement some HTML5 XHR v2 capabilities, see this enhancement request and this discussion.
To fix the blank PDF issue in post request to get stream data like PDF, we need to add response type as 'arraybuffer' or 'blob' in request. Hope this will save you a few hours and spare you from a headache.
Solution was this:. Include download. This snippet is for angular js users which will face the same problem, Note that the response file is downloaded using a programmed click event. If you have to work with file-stream so no physically saved PDF like we do and you want to download the PDF without page-reload, the following function works for us:. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow.
Learn more. Asked 11 years, 10 months ago. Active 1 year, 7 months ago. Viewed k times. I have an action class that generates a PDF. The contentType is set appropriately.
Mike Nayn Nayn 3, 8 8 gold badges 36 36 silver badges 47 47 bronze badges. Add a comment. Active Oldest Votes. Community Bot 1 1 1 silver badge. Mayur Padshala Mayur Padshala 1, 2 2 gold badges 18 18 silver badges 19 19 bronze badges. Does it work on chrome? I can only see a blank pdf. Yes, it does work on all modern browsers. URL window. Browser Compatibility. View Demo. Related Articles. Add Comments. Thank you for the feedback. The comment is now awaiting moderation.
You will be notified via email when the author replies to your comment.
0コメント