Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old May 30th, 2004, 08:29 AM
ofricker ofricker is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2004
Posts: 7 ofricker User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #2  
Old May 30th, 2004, 09:24 AM
DevCoach DevCoach is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Feb 2004
Location: London, England
Posts: 1,585 DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Weeks 4 Days 2 h 58 m 23 sec
Reputation Power: 1372
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

Reply With Quote
  #3  
Old May 31st, 2004, 04:49 AM
ofricker ofricker is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2004
Posts: 7 ofricker User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #4  
Old May 31st, 2004, 06:33 AM
DevCoach DevCoach is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Feb 2004
Location: London, England
Posts: 1,585 DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Weeks 4 Days 2 h 58 m 23 sec
Reputation Power: 1372
You have a typo in your form declaration - it should be "enctype"

Dave - The Developers' Coach

Reply With Quote
  #5  
Old May 31st, 2004, 01:21 PM
ofricker ofricker is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2004
Posts: 7 ofricker User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Cheers, got it working
Greets
Oli

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Store file from web form in db

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap