The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
Store file from web form in db
Discuss Store file from web form in db in the Python Programming forum on Dev Shed. Store file from web form in db Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

May 30th, 2004, 08:29 AM
|
|
Registered User
|
|
Join Date: May 2004
Posts: 7
Time spent in forums: < 1 sec
Reputation 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:
Quote: 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
|

May 30th, 2004, 09:24 AM
|
|
Contributing User
|
|
Join Date: Feb 2004
Location: London, England
|
|
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:
Quote: 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
|

May 31st, 2004, 04:49 AM
|
|
Registered User
|
|
Join Date: May 2004
Posts: 7
Time spent in forums: < 1 sec
Reputation 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 Quote: | <form action="/cgi-bin/process_form.py" entype="multipart/form-data" method="post" name="postform"> |
Anymore tips?
Ta
Oli
|

May 31st, 2004, 06:33 AM
|
|
Contributing User
|
|
Join Date: Feb 2004
Location: London, England
|
|
|
You have a typo in your form declaration - it should be "enctype"
Dave - The Developers' Coach
|

May 31st, 2004, 01:21 PM
|
|
Registered User
|
|
Join Date: May 2004
Posts: 7
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
Cheers, got it working
Greets
Oli
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|