|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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:
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
|
|||
|
|||
|
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:
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 |
|
#3
|
|||
|
|||
|
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:
Anymore tips? Ta Oli |
|
#4
|
|||
|
|||
|
You have a typo in your form declaration - it should be "enctype"
Dave - The Developers' Coach |
|
#5
|
|||
|
|||
|
Cheers, got it working
Greets Oli |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Python Programming > Store file from web form in db |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|