On Feb 26, 2013, at 9:44 AM, dalia <
dali...@gmail.com> wrote:
> Retrieving from database - now I want the file to be downloaded as an excel (.xls) file
>
> def _get_report(self):
> tbl = model.ReportFiles
> file = meta.Session.query(tbl).get(id=1)
> contentstr = file.report_file
> response.content_type = 'application/vnd.ms-excel; charset=utf-8'
> response.headers['Content-disposition'] = 'attachment; filename = %s' %file.file_name
> response.content_length = len(contentstr)
> print "Length = %s" %len(contentstr)
> return contentstr
>
>
> This downloads the .xls file fine. The print statement prints the file size as 5632. The download dialogue says the file size is 5.5KB. But when I open the file, it says 'file type not in right format'. When I open it in notepad, it is a 0 KB file. When I list the file in the directory, it is a 0KB file.
1. when you open the file from the filesystem using the Python open() call, in preparation for persisting it to the database, make sure you're using binary mode, open("myfile.xls", "rb"), because you're on windows. It's possible the files are being stored with modified line endings otherwise.
2. the "notepad" application isn't appropriate for displaying an .xls file.