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?

1
  • 1
    stackoverflow.com/questions/20361216/… 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. Commented Apr 10, 2014 at 1:38

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

1 Comment

You can't trigger a file download with $.ajax though, can you?
-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.

4 Comments

As this does not answer the question this should be a comment. That IS an interesting link though. Thank you.
@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.
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.
@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: stackoverflow.com/questions/15994554/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.