|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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 |
|
#2
|
||||
|
||||
|
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. |
|
#3
|
|||
|
|||
|
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. |
|
#4
|
||||
|
||||
|
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. |
|
#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 |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Python Programming > Help with uploading a file |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|