I have a link on my page on click of which I am trying to generate a PDF document and then show the "Open - Save" prompt on the browser.
My HTML (reactjs component) has the below code where onclick
calls the _getMyDocument
function which then calls a Webapi method.
<div className="row">
<a href="#" onClick={this._getMyDocument.bind(this)}>Test Link</a>
</div>
_getMyDocument(e) {
GetMyDocument(this.props.mydata).then(()=> {
}).catch(error=> {
});
My Controller has the below code
[HttpPost]
[Route("Generate/Report")]
public IHttpActionResult GetMyReport(MyData myData)
{
byte[] myDoc = MyBusinessObject.GenerateMyReport(myData);
var result = new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new ByteArrayContent(myDoc)
};
result.Content.Headers.ContentDisposition =
new ContentDispositionHeaderValue("attachment")
{
FileName = "MyDocument.pdf"
};
result.Content.Headers.ContentType =
new MediaTypeHeaderValue("application/octet-stream");
var response = ResponseMessage(result);
return response;
}
Currently all the code executes but I don't get the file PDF download prompt. What am I doing wrong here?
Response object on success from the ajax call lokks like below
The classic download confirmation prompts in Firefox let you control the browser's download flow. You can allow or cancel downloads, select the save location, or open the file with a different program.
HTML: Use the anchor element download attribute to prompt the user to download a resource. The download attribute on an anchor tag pops up a Save dialog to download the resource, instead of navigating to it. e.g. Using an attribute value, such as download="cat-1.
As long as you have the ability to edit the HTML code of the page, you can use a simple HTML tweak that will allow you to control when the file download prompt appears if the user clicks a link or a button. A special download attribute can be used inside of an <a href> tag that will tell the browser to download the file instead of navigating to it.
Since the objective of this post is just to let you know how to download files from the command prompt, you simply need to remember this basic Wget for Windows command: To use this command, open your Windows command prompt (click start > type cmd.exe on the search field then press “Enter”).
This is a good thing because you no longer need to install another software or program just to download some text files, PDF documents, MP3s, FLV and MP4 videos, etc. However, there are also limitations to what you can do so you might want to consider learning how to download files from the command prompt in Windows.
Command Prompt Portable allows you to have a custom Command Line setup available on any computer you’re working on. It is a lightweight, open source application available to Windows users (Windows XP, Vista, 7, 8, 10, 11). Follow the instructions below and I’ll show you how to download Command Prompt on your PC for free.
Your response from the server looks good. The missing part is that you are not handling this response from the client side in the correct way.
Lets assume that your resource url object looks like below in js. (i.e. you already know the resource url, if you don't know it yet, then you will need a separate call to the server to know the download url)
response.downloadUrl = app/media/fileId/document.pdf
All you need to do is set,
window.location = item.downloadUrl;
This will cause the browser to request a resource from the server, the response from the server must include Content-Disposition:attachment;
. It will cause the browser to show the download dialog.
P.S. I have recently worked on a similar functionality. If you have questions please ask.
When you want to force the browser to show the download prompt for some files (or resources), you must include Content-Disposition:attachment;
in the response header (which you already did).
A simple way to do this is on your server to use GET method, and receive your data by query parameters.
Then in your client app you just have to create a classic link:
<a href="http://your-server.com/your-resource?param1=123¶m2=456">Test Link</a>
Or use that simple js script if your want to manage it from your script logic:
window.open("http://your-server.com/your-resource?param1=123¶m2=456");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With