On Tue, Feb 26, 2013 at 2:44 PM, dalia <dalia....@gmail.com> wrote:
> Hi,
>
> My intention is - to store .xls, .doc and .pdf files (with images in them)
> to store in a Oracle database and retrieve them. I'm using SQLAlchemy
> declarative code. The problem is, the data gets stored in the database but
> when I try to fetch the data back, it comes out as 0KB. I'm explaining the
> code and actions below -
>
> The model -
>
> class ReportFiles(Base):
>   __tablename__ = 'report_files'
>   id = Column(Integer, primary_key=True)
>   report_file = Column(BLOB)
>   file_name = Column(Unicode(50))
>
>
> Storing in database -
>
> content = cStringIO.StringIO()
> def store_in_db():
>    session = meta.Session()
>    self._get_file(content)     ### This is a function which generates the
> file (.xls / .doc / .pdf) in the content
>    contentstr = content.getvalue()
>    tbl = model.ReportFiles()
>    tbl.file_name = "test file"
>    tbl.report_file = contentstr
>    session.add(tbl)
>    session.commit()
>
> After the store, if I query the database, I get this result - not sure if it
> has stored alright -
>
> select file_name, length(report_file) from report_files;
>
> Name   |  Length
> --------------------------
> test file |  5632
>
> select file_name, report_file from report_files;
>
> Name | file
> ------------------
> test file | (BLOB)
>
> This doesn't look right because instead of the file, it just shows (BLOB)
>
> 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.
>
> Can somebody please tell me what is wrong here? Looks like the file doesn't
> get stored properly? Or am I missing something obvious? Or do I need to
> handle BLOB types differently?
>

I don't know what web framework you are using, but in pyramid I think
you would set the "response.body" attribute to your contentstr, rather
than returning it. Does that make any difference?

Simon

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to