kmb
kmb

Reputation: 871

How to download html file from database using GRAILS?

I'm trying to implement simple upload/download file to/from DB functionality using GRAILS. Everything seems to work fine except text/html files.

Domain:

class Content {
    byte[] file;
    //filesProperties
    String contentType
    String fileName
    Long size

    static constraints = {
        file maxSize: 1024 * 1024 * 20, nullable: true; //20MB
        fileName nullable:true
        size nullable:true
        contentType nullable:true   
    }
}

Action:

def getFile()
{
    def content = Content.get(params.id)
    response.contentType = content.contentType
    response.setHeader("Content-disposition","attachment;filename="+content.fileName)
    response.contentLength= content.size
    response.outputStream << content.file
    response.outputStream.flush()

}

After uploading html file, everything seems good on database site. Simple SELECT shows that file is uploaded already. When I try to download html file I get Page not found (in Chrome net::ERR_FILE_NOT_FOUND). Action getFile() works good for any other content-type.

Does exist any possibility to force download (not displaying it in browsers) html file this way?

BTW: Why displaying it (response.header without "attachment" value in Content-disposition key) does not work too?

Upvotes: 0

Views: 784

Answers (1)

user800014
user800014

Reputation:

You can try to force the download:

response.setHeader("Content-Type: application/force-download");

Upvotes: 1

Related Questions