#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Posts
    7
    Rep Power
    0

    Store file from web form in db


    Hello

    I'm a newbie to python and have the following problem:

    What's the best way storing files from web forms straight into a database?

    I have a web form where users attach a file. The information and the file have to be stored in mysql. The form and the script work fine when I run it from the web server. As soon as I submit the form from another pc an error occurs:

    requestfile undefined, builtin open = <type 'file'>, form = FieldStorage(None, None, [MiniFieldStorage('lr_f...ieldStorage('submitButtonName', 'Send Request')]), form.getvalue = <bound method FieldStorage.getvalue of FieldStor...eldStorage('submitButtonName', 'Send Request')])>, ).read undefined

    IOError: [Errno 2] No such file or
    directory: 'C:\\temp\\test\\test.txt'
    args = (2, 'No such file or directory')
    errno = 2
    filename = r''C:\\temp\\test\\test.txt'
    strerror = 'No such file or directory'
    The error seems logical, the path from the form does not exist on the web server.

    I'm using the following code:
    Code:
    ...
    form = cgi.FieldStorage()
    file = open(form.getvalue('filename'), 'rb').read()
    ...
    Does anyone have a tip? (Guess an option would be to save the file to the web server's hd first and then insert it into the db.)
    Thx in advance
    Oli
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    You do not retrieve CGI file transfers like that. You can't open the file with open(...), because the file will not be on the same machine as the server.

    Here is what the docs for the cgi module say:

    If a field represents an uploaded file, accessing the value via the value attribute or the getvalue() method reads the entire file in memory as a string. This may not be what you want. You can test for an uploaded file by testing either the filename attribute or the file attribute. You can then read the data at leisure from the file attribute:


    Code:
    fileitem = form["userfile"]
    if fileitem.file:
        # It's an uploaded file; count lines
        linecount = 0
        while 1:
            line = fileitem.file.readline()
            if not line: break
            linecount = linecount + 1
    The file upload draft standard entertains the possibility of uploading multiple files from one field (using a recursive multipart/* encoding). When this occurs, the item will be a dictionary-like FieldStorage item. This can be determined by testing its type attribute, which should be multipart/form-data (or perhaps another MIME type matching multipart/*). In this case, it can be iterated over recursively just like the top-level form object.
    So you can replace
    file = open(form.getvalue('filename'), 'rb').read()
    with
    file = form.getvalue('fieldname')

    replacing 'fieldname' with the appropriate field name.

    Dave - The Developers' Coach
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Posts
    7
    Rep Power
    0
    Hi Dave

    Thanks for your reply. Unfortunately I don't get it working...

    The aim is to store the file from the web form as a BLOB into the db.

    My script doesn't even get passed the "if fileitem.file:" line, so no file gets recognized. I'm not sure if it's got anything to do with the html web form. The action is defined as
    <form action="/cgi-bin/process_form.py" entype="multipart/form-data" method="post" name="postform">
    Anymore tips?
    Ta
    Oli
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    You have a typo in your form declaration - it should be "enctype"

    Dave - The Developers' Coach
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Posts
    7
    Rep Power
    0
    Cheers, got it working
    Greets
    Oli

IMN logo majestic logo threadwatch logo seochat tools logo