SQLFORM automatically uploads the db.files.file to a file in uploads/
that means the input stream is empty after the load.

Do you want this to happen or just want it to pass to load_to_db?

Here are some options and one may be the one that you want:

Instead of:

  form = SQLFORM(db.files, fields = ['file'])
  if form.accepts(request.vars, session):
    fileuploaded = "File uploaded"
    sheetnr = load_to_db(request.vars.file.file.read())

option 1

  db.files.file.uploadfolder=False
  form = SQLFORM(db.files.file')
  if form.accepts(request.vars, session):
    fileuploaded = "File uploaded"
    sheetnr = load_to_db(request.vars.file.file.read())

option 2

  form = SQLFORM(db.files, fields = ['file'])
  if form.accepts(request.vars, session):
    fileuploaded = "File uploaded"
    request.vars.file.seek(0)
    sheetnr = load_to_db(request.vars.file.file.read())

option 3

  form = SQLFORM(db.files, fields = ['file'])
  if form.accepts(request.vars, session):
    fileuploaded = "File uploaded"
    request.vars.file.seek(0)
    filename, stream = db.files.file.retrieve(form.vars.file)
    sheetnr = load_to_db(stream.read())



On Mar 26, 8:49 am, Mariusz Zieliński <[email protected]> wrote:
> Hello,
>
> I got problem with reading from uploaded xls just after forms.accept.
> Code looks like this:
>
> program.py
> ---
> db.define_table('files',
>   SQLField('file', 'upload'),
>   SQLField('filename')
> )
>
> default.py
> ----
> def upload():
> #  first form - working
> #  form = FORM('Upload file: ',
> #              INPUT(_name='file', _type='file'),
> #              INPUT(_type='submit'))
>
> #  second form - not working
>   form = SQLFORM(db.files, fields = ['file'])
>
>   fileuploaded = ''
>   sheetnr = 0
>   if form.accepts(request.vars, session):
>     fileuploaded = "File uploaded"
>     sheetnr = load_to_db(request.vars.file.file.read())
>   return dict(form = form, status = fileuploaded, sheetnr = sheetnr)
>
> def load_to_db(a):
>   book = xlrd.open_workbook(file_contents = a)
>   return book.nsheets
>
> Form not connected to DB works, and file can be read without any
> problems. When I'm trying to do the same with saving file on disk I
> receive error from xlrd: "TypeError: coercing to Unicode: need string
> or buffer, NoneType found". Is there any way to do this ? Or maybe
> there's some way to read path to the newly uploaded file so I can pass
> filename instead of file_content to xlrd ?
>
> Thank you for help in advance! :)
>
> M

Reply via email to