3

I recently learned that I can't download more than 1 file with a single HTTP request in ruby on rails except I do it with AJAX request. I'm now trying to figure out how to do that.

(I'm using Prawn as PDF creator with Ruby on RAils 3) I have an action in my controller that render a PDF and use send_data

def download_quote          
pdf = QuotesPdf.new(params)
send_data pdf.render, filename: "foo.pdf",
                    type: "application/pdf",
                    disposition: "download"
end

Then I have a button in my HTML view that has this working Jscript code

// AJAX, download
function ajaxRequest(){
    $.ajax({
      type: 'POST',
      url: '/download_quote/126',
      success: function(data){
        alert(data);
      }
    });
    return false;
  }

  $("#mydownload").click(ajaxRequest);

When I click "#mydownload" after few seconds took for rendering PDF, I receive a successful alertbox with inside data, I think, all the PDF file.

Question is: How can I transform data in a pdf file and automatically download it?

Riccardo Neri
  • 790
  • 1
  • 11
  • 24

1 Answers1

2

I don't think "download" is a valid content disposition type. Try using "attachment" instead.

Content Disposition

def download_quote          
    pdf = QuotesPdf.new(params)
    send_data pdf.render, filename: "foo.pdf",
        type: "application/pdf",
        disposition: "attachment"
end
Miguel-F
  • 13,450
  • 6
  • 38
  • 63
  • Well, I will check! Anyway I always used download without error so far...but how this can change the javascript side? – Riccardo Neri Oct 26 '12 at 12:57
  • I'm not really sure what you are trying to do but I wanted to point out that download is not a valid content disposition. It should be inline or attachment. You can try both of those to see if it behaves any differently. Inline will download the file and immediately try to display in browser. Attachment will prompt the user to save or open. Since you are making an AJAX call I'm not sure how those will come into play. – Miguel-F Oct 26 '12 at 13:04
  • 1
    @RiccardoNeri see this similar question: [http://stackoverflow.com/questions/1999607/download-and-open-pdf-file-using-ajax](http://stackoverflow.com/questions/1999607/download-and-open-pdf-file-using-ajax) – Miguel-F Oct 26 '12 at 13:07