5

I have a route /reports/foo-report/rendered/ which will return a file download (Using the Content-Disposition header). As recommended by REST, whether the report is returned as a pdf or powerpoint depends on the request's Accept header.

What are my options for setting this header when triggering a file download? I obviously cannot use XmlHttpRequest, a link, or a form. I'm currently using the awesome jquery.fileDownload but it doesn't seem to support this either.

Is there any way to do this in a proper RESTy fashion in browsers?

George Mauer
  • 117,483
  • 131
  • 382
  • 612
  • 1
    http://stackoverflow.com/questions/20361216/how-can-i-set-the-accept-header-for-a-link-on-a-web-page proposes using AJAX anyway and returning a data URI, which you can then redirect to to trigger the download. It might be difficult for very large files, but it should work for average PDFs and PPTs. – qternion Apr 10 '14 at 01:38

2 Answers2

-1

I'm not familiar with jquery.fileDownload, but if you have access to the $.ajax({}) call you can set specific headers:

$.ajax({
    headers: { 
        Accept : "text/plain; charset=utf-8",
        "Content-Type": "text/plain; charset=utf-8"
    },
    data: "data",
    success : function(response) {
        ...
    }
})

as posted here.

Community
  • 1
  • 1
klugerama
  • 3,312
  • 19
  • 25
-1

A curious question indeed, unfortunately I can't think of a way to do this.

But I did find Django arguing that format suffixes should be considered an acceptable pattern. Whilst this doesn't give you a way to use the Accept header, it does suggest an alternative (arguably) RESTy approach.

rcambrj
  • 592
  • 3
  • 8
  • As this does not answer the question this should be a comment. That IS an interesting link though. Thank you. – George Mauer Apr 07 '14 at 18:50
  • @GeorgeMauer I believe it does answer the question "Is there any way to do this in a proper RESTy fashion in browsers?", suggesting an arguable RESTy approach. – rcambrj Apr 17 '14 at 09:50
  • Hmm, the question was explicitly about setting the Accept header. I now understand that it seems impossible. If you want to rewrite the answer to cite a source on this and @ me I'll mark it as correct. – George Mauer Apr 17 '14 at 14:33
  • @GeorgeMauer Feel free to edit yourself with any source you feel is appropriate. Alternatively, mega.co.nz have a fancy download mechanism which might just work with an API which accepts an `Accept:` header, details: http://stackoverflow.com/questions/15994554/download-files-like-mega-co-nz – rcambrj Apr 28 '14 at 08:40