Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
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 July 1st, 2004, 08:01 PM
roypython roypython is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 71 roypython User rank is Lance Corporal (50 - 100 Reputation Level)roypython User rank is Lance Corporal (50 - 100 Reputation Level)roypython User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 1 h 20 m 49 sec
Reputation Power: 5
Help with uploading a file

Hello.
I have a problem that I banged my head against the wall for many days but I can't find solution, your help is highly appreciated.
The problem is:
The user can upload a file from html form.
On the servera cgi script runs a functionthat reads all the form data and put it into a dictionary.
Soemetimes when I'm uploading a file(it happens usually with pdfs),
the dictionary is totally ruined.
and I can't use it.
My guess is that it has to do with the uploaded file's content.
I tried to serialize the content in order to make it "python safe" but it just won't work.
The function that I'm using is:
Code:
 
def baseReadVals(self):
        form = cgi.FieldStorage()
        resDct = {}
        for key in form.keys():
            lst = form.getlist(key)
            if len(lst) == 1:
              x = form[key]
              if x.file and  x.filename!=None: 
                 # If it is a file, create a key wth the name key+'_FILENAME'
                 keyName = (str)(key)+'_FILENAME'
                 # the uploaded file content goes into resDct[keyName]
                 resDct[keyName] = form[key].filename
                 #resDct[key] = serialiaze(form[key].value)
              else:
                 resDct[key] = form[key].value
            else: resDct[key] = lst
        return resDct                     
  


If you run it you'll see that for some uploaded files, the dictionary resDct
it totally messed up.
Thank you
roy

Reply With Quote
  #2  
Old July 2nd, 2004, 01:49 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,536 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 3 m 4 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
There is an example of uploading binary files in CGI in my article on devshed:

http://www.devshed.com/c/a/Python/Python-on-the-Web/

You should also read the sticky at the top of this forum regarding how to ask a question aka you need to use code tags!

Later,

Mark.
__________________
programming language development: www.netytan.com Hula


Reply With Quote
  #3  
Old July 2nd, 2004, 05:18 AM
roypython roypython is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 71 roypython User rank is Lance Corporal (50 - 100 Reputation Level)roypython User rank is Lance Corporal (50 - 100 Reputation Level)roypython User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 1 h 20 m 49 sec
Reputation Power: 5
Thanks Mark for the quick reply.
I read your article, it's great, very clear and intuitive.

In your article, you give an example of uploading a file and then saving it to the hard drive.
I am , on the other hand, trying to keep the content of the file in a dictionary [ and then save this dictionary, serialized to the DB ].
The dictionary contains all the from data ( cgi.FieldStorage() ).
The problem, as I mentioned before , that with some files, the dictionary turns into rabbish. Immidiately, after I create the dictionary, I tried to print it to view it's content, and with some files, the dictionary is rabbish.

Reply With Quote
  #4  
Old July 2nd, 2004, 06:26 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,536 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 3 m 4 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
Ok, so you want to store the values between submitions? If that is what you want then you're going to need a way of storeing and reloading the dictionary - this would normally be done with a relasional database but not 100% nessasary.

In which case you should look at the Pickel, Shelve and DBM modules and pick which one you want to use.

http://www.python.org/doc/2.3.4/lib/module-pickle.html
http://www.python.org/doc/2.3.4/lib/module-cPickle.html
http://www.python.org/doc/2.3.4/lib/module-shelve.html

http://www.python.org/doc/2.3.4/lib/module-anydbm.html

Also, do you want to replace the values in the dictionary with new ones, or just add values to the dictionary?

You could also use my 'Net' module which might make things easier than if you used the CGI modules FieldStorage module; the down side to this being that their are no docs available yet. But worth a look:

http://forums.devshed.com/t129666/s.html #latest version
http://forums.devshed.com/t126942/s.html #older version + examples.

Thanks for what you said about the article. Means a lot to hear that somone enjoyed my work!

Hope this helps,

Mark.

Reply With Quote
  #5  
Old July 4th, 2004, 09:07 PM
roypython roypython is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 71 roypython User rank is Lance Corporal (50 - 100 Reputation Level)roypython User rank is Lance Corporal (50 - 100 Reputation Level)roypython User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 1 h 20 m 49 sec
Reputation Power: 5
Hi Mark.
Thanks for the quick reply ,for the rescources and for your code.
I will print it and read it thoroughly, I'm sure i'll learn a lot from that.
I decided after spending too much valuble time on trying to save the file's content in a dictionary, to save it in a temp directory, and in the dictionary i'll keep just the file name, that it'll be used to reference the file in the temp dir.
Also i'll write scheduled garbage collector script, that will check the last accessed date for every file in the tmp dir, and if it is more then X time, it'll delete it.
It's not very elegant, but I think that it'll be faster to get it to work.
Thanks again Mark
Roy

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Help with uploading a file


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 | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
Stay green...Green IT