CFML_Developer
CFML_Developer

Reputation: 1605

Can I retrieve a file from server with GET in rest

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.

Upvotes: 1

Views: 1431

Answers (2)

CFML_Developer
CFML_Developer

Reputation: 1605

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>

Upvotes: 1

Adam Cameron
Adam Cameron

Reputation: 29870

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

Upvotes: 2

Related Questions