Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prompt file download

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

enter image description here

like image 397
San Avatar asked Nov 17 '16 18:11

San


People also ask

What is a download prompt?

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.

How do I prompt a download in HTML?

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.

How can I control when the file download prompt appears?

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.

How to download files using Wget for Windows command?

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”).

Is it possible to download text files from the command prompt?

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.

What is command prompt portable and how to use it?

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.


2 Answers

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).

like image 65
ScrapCode Avatar answered Oct 01 '22 18:10

ScrapCode


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&param2=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&param2=456");
like image 35
Thibaud Sowa Avatar answered Oct 01 '22 18:10

Thibaud Sowa