Hello Samuele,

[...]
> If your HTML form has field called e.g. “foo” of type “file” you will then be 
> able to do:
>
> req.form[‘foo’].filename to obtain the name.
>
> req.form[‘foo’].file to obtain the open file object, so e.g. you can
> use req.form['foo'].file.read() to read its whole content ;-) (but be
> careful in this case because if the file that was sent is too big it
> would fill-up all the memory :-) ).

call me dumb, but I keep getting errors (TypeError: 'NoneType' object is
not iterable).  I've even compared the current SimulatedModPythonRequest
from git with 1.1.0, and there is nothing relevat, to my eyes.

I attach my smallest example, trying to follow your hints.  I'm sorry to
bother you with it, but SimulatedModPythonRequest is an Invenio-specific
implementation and nobody else in the world has this knowledge ;-(

Can you see where is the error in this small example?

Thanks,

Ferran
#!/usr/bin/python
# -*- coding: utf-8 -*-

import os
import sys
import pprint

sys.path.append(os.path.expanduser('~/lib/python/'))
from invenio.config import CFG_SITE_NAME
from invenio.webpage import pageheaderonly


def index(req):
    req.content_type = 'text/html'
    req.send_http_header()
    script = 'testupload.py'

    title = '%s: Upload test' % (CFG_SITE_NAME)
    pageheader = pageheaderonly(title)

    form = '''
 <div class="pagebody">

  <h2>%(title)s</h2>

  <p>
  <blockquote>
   Testing file upload under SimulatedModPythonRequest
   <form enctype="multipart/form-data" action="./%(script)s/upload" method="post">
    <input type="file" name="file" />
    <br />
    <input type="submit" value="Upload" />
   </form>
  </blockquote>
 </class>
''' % {
        'title': title,
        'script': script,
        }

    return pageheader + form


def upload(req):
    try:
        filename = req.form['file'].filename
    except:
        filename = 'no filename'

    try:
        filecontents = req.form['file'].file.read()
    except:
        filecontents = 'no file contents'

    page = '''<pre>
 filename: [%(filename)s]
 contents: [%(contents)s]
</pre>
''' % {
         'filename': filename,
         'contents': contents
         }

    return page

Reply via email to