|
Hi
I am trying to download a file from a database. I have searched the forum and could not find what i was looking for. I have a url link in the gsp pointing to the download action in the controller. Firefox opens the file browser, and asks me to choose location to save file. When i open the downloaded file it contains error messages from eclipse. What am i doing wrong? def download = { /* fileObj is an instance of the domain class */ FileUpload fileObj = FileUpload.get(params.id) def file = new File(fileObj.id) String sFileName = fileObj.fileName response.setHeader("Content-Type", "application/octet-stream;") response.setHeader("Content-Disposition", "attachment; filename=\"" + sFileName + "\"") response.setHeader("Content-Length", "${file.size()}") response.outputStream << file.newInputStream() redirect(action:list) } |
From looking at the code, I assume that you only keep the filename in the database while the file itself is an ordinary file with fileObj.id as name. Is the file you're sending in the first place correct ('println file.getAbsolutePath() + file.exists()')? It would also be better to explicitly set the path of the files iso of depending on the working dir of your webserver. Ivo |
Thanks for replying. The file itself is stored as blob in the database. e.g +-----------+--------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-----------+--------------+------+-----+---------+----------------+ | id | bigint(20) | NO | PRI | NULL | auto_increment | | version | bigint(20) | NO | | | | | file_name | varchar(255) | NO | | | | | the_file | longblob | NO | | | | +-----------+--------------+------+-----+---------+----------------+ How can i pass that file to the client from the database? |
|
Hi CarrolJ,
It doesn't look like anywhere in your code you actually take the contents of the blob ("the_file") and send it to the user. I see you do something like def file = new File(fileObj.id) but that doesn't actually create a file with the contents from the database (unless Grails is doing some magic I'm not aware of). Maybe you can try something like: response.outputStream << fileObj.theFile or something along those lines. Cheers, Josh --- Josh Reed ANDRILL IT Specialist Tel.: (952) 681-2227 Cell: (612) 308-4011 Email: [hidden email] ANDRILL Science Management Office 126 Bessey Hall University of Nebraska-Lincoln Lincoln, NE 68588-0341 Fax: (402) 472-6723 URL: http://andrill.org On Apr 7, 2008, at 9:12 AM, CarrollJ wrote: > > > > houbie wrote: >> >> >> CarrollJ wrote: >>> >>> I am trying to download a file from a database. >>> ... >>> FileUpload fileObj = FileUpload.get(params.id) >>> def file = new File(fileObj.id) >>> >> From looking at the code, I assume that you only keep the filename >> in the >> database while the file itself is an ordinary file with fileObj.id as >> name. Is the file you're sending in the first place correct ('println >> file.getAbsolutePath() + file.exists()')? >> It would also be better to explicitly set the path of the files iso >> of >> depending on the working dir of your webserver. >> >> Ivo >> > > Thanks for replying. > > The file itself is stored as blob in the database. e.g > +-----------+--------------+------+-----+---------+----------------+ > | Field | Type | Null | Key | Default | Extra | > +-----------+--------------+------+-----+---------+----------------+ > | id | bigint(20) | NO | PRI | NULL | auto_increment | > | version | bigint(20) | NO | | | | > | file_name | varchar(255) | NO | | | | > | the_file | longblob | NO | | | | > +-----------+--------------+------+-----+---------+----------------+ > > How can i pass that file to the client from the database? > > -- > View this message in context: http://www.nabble.com/Download-a-file-from-database-tp16537389p16537458.html > Sent from the grails - user mailing list archive at Nabble.com. > > > --------------------------------------------------------------------- > To unsubscribe from this list, please visit: > > http://xircles.codehaus.org/manage_email > > --------------------------------------------------------------------- To unsubscribe from this list, please visit: http://xircles.codehaus.org/manage_email |
|
In reply to this post by John Carroll
That would be something like: response.outputStream << fileObj.the_file.binaryStream |
|
In reply to this post by Josh Reed
Hi Josh,
Thanks for the reply. That is my problem. It is not getting any data from the database. The method file.length() returns a size of 0. I will try - response.outputStream << fileObj.theFile - and post a reply. Regards, Carrollj
|
Hi Josh I can download from the database now. I removed the code where i was trying to make a file. response.outputStream << fileObj.theFile works fine. Thanks for the help all ![]() CarrollJ
|
| Powered by Nabble | Edit this page |
