1

Can I return a file with my get request? I want to return a word document to calling angularJS service through REST GET method. Not sure if it is even possible.

CFML_Developer
  • 1,565
  • 7
  • 18
  • 1
    Sure, it's possible, but you'll need to post something that you've done for us to work off of. – Matt Busche Jul 03 '14 at 12:09
  • Sorry Matt. See my last line. I am not even aware this can be done. As far as I understand, I can return only string (json/xml) representation back. So possibly binary format? All I am asking is a help conceptually not the code. – CFML_Developer Jul 03 '14 at 12:14
  • Here's a Java answer http://stackoverflow.com/questions/3496209/input-and-output-binary-streams-using-jersey – Matt Busche Jul 03 '14 at 12:15

2 Answers2

2

You're a bit light on detail, so I'm gonna be a bit light on answer.

A REST request is just... a request. The REST side of things is more the way URLs are defined that what actually happens in the request process itself, which is all still vanilla HTTP.

So, same as with any GET request, if you want to return binary data, set the headers appropriately (<cfheader>), then return the file (<cfcontent>).

Adam Cameron
  • 29,677
  • 4
  • 37
  • 78
  • As Matt asked for some code, I didn't had any idea if this could be done at all. Then Matt said that it is possible and I started like a headless chicken :) I was reading your post, though not any direct relation (http://cfmlblog.adamcameron.me/2014/04/coldfusion-rest-services-and.html), but thought what if I set headers & content of response headers. Atleast I will have to show you something then even if wrong :) And about details, that is as much I have, I have complete physical path of the file, need to return file back to client. – CFML_Developer Jul 03 '14 at 12:39
  • Give it a go: your computer won't explode if you get it wrong ;-). I don't have any example code to hand, I'm afraid, so would have to knock it together to show you. And stopped and thought... it's perhaps you who should be doing that ;-) – Adam Cameron Jul 03 '14 at 12:44
1

So this is how I did it, luckily I got this: http://smithcustomdev.com/2010/10/20/download-file-to-browser-using-cfscript/ All I have to do was make the method remote and listen to REST service

<cfscript>
    private void function serveFile(string filePath){
        var fileContent = fileRead(expandPath(filePath));   

        var context = getPageContext();
        context.setFlushOutput(false); 

        var response = context.getResponse().getResponse();
        response.reset();
        response.setContentType("text/csv");   
        response.setContentLength(len(fileContent));       
        response.setHeader("Content-Disposition","attachment; filename=#listLast(filePath,'\')#");

        var out = response.getOutputStream();     
        out.write(ToBinary(ToBase64(fileContent)));       
        out.flush();       
        out.close();
    }
</cfscript>
CFML_Developer
  • 1,565
  • 7
  • 18
  • Content type of csv, isn't ideal or correct, as you're not returning a csv file. – Busches Jul 04 '14 at 00:08
  • @Busches, more than the code it is concept (which I found out works very well). I changed this function little bit to allow me send back MS-word file. I also changed function according to my requirement, accept a id, fetch the file name from DB and work on that. The blog entry also helps to allow you to determine mime type dynamically. – CFML_Developer Jul 04 '14 at 05:46
  • Link is dead - long live the link in the [wayback machine](http://web.archive.org/web/20141111063541/http://smithcustomdev.com/2010/10/20/download-file-to-browser-using-cfscript/). – MT0 Oct 06 '15 at 09:42