grails, how to force the browser to download a file -


in controller, try browser download file when user clicks on link:

    render( contenttype: 'text/csv', text: output); 

this works in chrome, not work in ie or safari, show data. also, shows file name number (which happened id on url, such www.me.com/show/1

apparently. way fix download convert octet stream. can done in htaccess file, im not using apache. there way in grails? imagine common scenario.

in php, 1 might this:

  header('content-disposition: attachment; filename="downloaded.csv"'); 

any ideas?

after reading 2 repsonses below (may thanks!) worked:

response.setheader "content-disposition", "attachment; filename=report.csv"     response.contenttype = 'text/csv'     response.outputstream << output     response.outputstream.flush() 

the amazing thing take string , use << write output stream. going try work out how turn string stream.

class downloadcontroller {     def download(long id) {         document documentinstance = document.get(id)         if ( documentinstance == null) {             flash.message = "document not found."             redirect (action:'list')         } else {             response.setcontenttype("application/octet-stream")             response.setheader("content-disposition", "attachment;filename=\"${documentinstance.filename}\"")              def outputstream = response.getoutputstream()             outputstream << documentinstance.filedata             outputstream.flush()             outputstream.close()         }     } } 

refer site more


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -