JavaProgrammer
JavaProgrammer

Reputation: 85

File download feature in grails application

I am looking to create a file on the fly and offer a download link to the user in a GRAILS application. I followed the approach from here. I have no errors however it doesn't seem to work. Here's my controller code.

`render (file: pptFile, fileName:'someppt.pptx', contentType: 'application/octet-stream')

Client side code makes an AJAX call to retrieve the file from server. It does not cause the server to force downloading of the file on the client (browser). Here's the client side code.

$.ajax({
type : 'POST',
url : '<<URL>>',
success: function(result) {
var uri = 'data:application/octet-stream;charset=UTF-8,' +   
encodeURIComponent(result);
window.open(uri, 'somePPT.pptx');
},
failure: function(){
alert ('failure')
}
});

Upvotes: 0

Views: 1476

Answers (3)

susi
susi

Reputation: 493

I like the solution using the render method from @railsdog !

A slightly other approach which I used so far was:

def controllerMethod() {
    ...
    File file = sepaXmlService.createTransfersFile(...)

    response.setContentType("application/xml")
    response.setHeader("Content-disposition", "attachment;filename=${file.getName()}")

    OutputStream out = response.getOutputStream()
    out.write(file.bytes)
    out.close()
    file.delete()
    return
    ...
}

In the view I use the following statement in the form:

<g:actionSubmit action="controllerMethod" class="btn" value="Get XML!" /></td>

I think it should also be possible to use a

<g:link controller="foobar" action="controllerMethod" class="btn">GetXML</g:link>

Upvotes: 0

JavaProgrammer
JavaProgrammer

Reputation: 85

okay. So I finally got this to work. As proposed by @railsdog and many others (This problem has been discussed on other threads in stackoverflow but the specific case I had was slightly different from those) I ended up writing to response directly from server and took out the AJAX call. The only reason I was doing an AJAX call was because I did not want to submit the current page that had the "generate file" functionality (There are many data elements on the page and I did not want to re-do the entire page just for downloading the file). So I ended up using an anchor tag with target as "_blank". Here's the code snippet

<a href="myControllerMethodToGenerateFileAndWriteToHTTPResponseDirectlyAsSuggestedByOthersInThisPost" 
target="_blank"/>

This actually opened a new page and did the submission to initiate the download. Problem solved. It's working fine in CHROME. :) Thanks guys!

Upvotes: 0

railsdog
railsdog

Reputation: 1501

Perhaps something akin to this (paraphrased, but used for downloading a json file):

def someControllerMethod() {
    def dlContent = someService.marshalJson()
    def contentType = "application/octet-stream"
    def filename = "someFilename.json"
    response.setHeader("Content-Disposition", "attachment;filename=${filename}")
    render(contentType: contentType, text: dlContent as JSON)
}

Upvotes: 3

Related Questions