Python file upload

Bjarne Christiansen bjarne_christiansen at hotmail.com
Wed Jun 26 06:48:59 EDT 2002


Thanks for your reply Richard!

I have changed the script to the following:
...
  item = form["filename"]
  if item.file:
    data = item.file.read()
    f = open("file1.jpg","wb")
    f.write(data)
...

But I still run into the same problem, any ideas?

~Bjarne


>From: Richard Barrett <R.Barrett at ftel.co.uk>
>To: "Bjarne Christiansen" <bjarne_christiansen at hotmail.com>,   
>python-list at python.org
>Subject: Re: Python file upload
>Date: Wed, 26 Jun 2002 09:56:33 +0100
>
>I think you will find that your calls to cgi.escape the incoming data are 
>the cause of your problem.
>
>You do not need to escape incoming multipart/form data. The documentation 
>for cgi.escape says:
>
><quote>
>Use this if you need to display text that might contain such characters in 
>HTML
></quote>
>
>which is clearly not your purpose in handling the incoming data, 
>particularly when it is binary data.
>
>At 08:08 26/06/2002 +0000, Bjarne Christiansen wrote:
>>Hi,
>>I have some problems uploading binary files though the web browser. It 
>>seems to work fine when uploading ASCII file but but binary files seems to 
>>be corrupted. The beginning of the binary file seems fine, but some data 
>>is missing....
>>
>>Here is my file upload script:
>>
>>#!c:\Python\python -d
>>
>>import cgi
>>
>>print "content-type: text/html\n\n"
>>
>>form = cgi.FieldStorage()
>>if not form:
>>  print """
>><form action="http://localhost/cgi-bin/upload.py" method="POST" 
>>enctype="multipart/form-data">
>><input type="file" name="filename">
>><input type="submit">
>></form>
>>  """
>>elif form.has_key("filename"):
>>  item = form["filename"]
>>  if item.file:
>>    data = item.file.read()
>>    print cgi.escape(data)
>>    data1 = cgi.escape(data)
>>    f = open("file1.jpg","wb")
>>    f.write(data1)
>>
>>Any help will be greatly appriciated!
>>
>>Best Regards,
>>Bjarne Christiansen
>>bjarne_christiansen at hotmail.com
>>
>>
>>_________________________________________________________________
>>MSN Photos is the easiest way to share and print your photos: 
>>http://photos.msn.com/support/worldwide.aspx
>>
>>
>>
>>--
>>http://mail.python.org/mailman/listinfo/python-list
>
>
>
>--
>http://mail.python.org/mailman/listinfo/python-list


_________________________________________________________________
Send and receive Hotmail on your mobile device: http://mobile.msn.com






More information about the Python-list mailing list