Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Problem while uploading a binary file using cgi module

18 views
Skip to first unread message

J. Wang

unread,
Feb 3, 2002, 9:49:59 PM2/3/02
to
I test the script from
http://www.python.org/doc/essays/ppt/sd99east/sld058.htm.

My problem is that it works fine with a text file, but failed everytime
with a binary file such as jpeg file, even a very small (5k) binary file.
I tried read(), readline(), readlines(). It just doesn't work. Every time
the FieldStorage() only read a very small portion of the binary file and
then stop.

what have I done wrong? By the way, i need to use it in a webpage, so
ftplib module is not a alternative option.

Thanks!

J. Wang

the code is like this:
#!e:/Python22/python.exe
import cgi

print 'Content-type: text/html\n'
form = cgi.FieldStorage()
if not form:
print """
<form action="test.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:
while 1:
data = item.file.readline()
fp = file('1.jpg','wb')
if data:
fp.write(data)
else:
break
fp.close()

Gerson Kurz

unread,
Feb 4, 2002, 8:22:55 AM2/4/02
to
On Sun, 3 Feb 2002 21:49:59 -0500, "J. Wang" <j...@cse.buffalo.edu>
wrote:

>
>My problem is that it works fine with a text file, but failed everytime
>with a binary file such as jpeg file, even a very small (5k) binary file.

Rather than writing

#!e:/Python22/python.exe

use

#!e:/Python22/python.exe -u

IIRC there is an entry in the FAQ for this, although I cannot find it
right now.

DeepBleu

unread,
Feb 4, 2002, 9:27:23 AM2/4/02
to
Wang
Please send the code indented next time. You know, you may know it inside
out. But poor readers like me do not know your intentions and indenting
makes it easier to read.

>
> elif form.has_key("filename"):
> item = form["filename"]

What are you trying to access by using item=form["filename"]?
try the following:
elif form.has_key("filename"):
if len(form["filename"].value) > 0: #Process if file is not an empty
file.
#byte value is
form["filename"].value
fp=open('1.jpg','wb') #you can also open this file in a defined
directory.
# make sure to use os.sep such as
"/" if necessary
# please note: access Byte value of files with
form["filename"].value;
#access file name itself using form["filename"].filename
fp.write(form["filename"].value)
fp.flush #good to use under certain conditions. check your
favourite python library doc
fp.close()
DeepBleu


Andrew MacIntyre

unread,
Feb 5, 2002, 7:26:35 AM2/5/02
to
On Sun, 3 Feb 2002, J. Wang wrote:

{...}

> the code is like this:
> #!e:/Python22/python.exe
> import cgi

{...}

Given that a text file works and a binary file doesn't, you are almost
certainly falling foul of the CRLF<->LF newline (text/binary) translation
used by default on most DOS derived environments (I'm not certain how
Cygwin handles this).

You don't say exactly what platform your Python is running on, however if
using the standard Windows binaries, look into the docs for the msvcrt
module (setmode()). I have something similar in development for OS2/EMX.
If Cygwin, you'll need to follow up with Cygwin folk (Jason Tishler
perhaps?)

--
Andrew I MacIntyre "These thoughts are mine alone..."
E-mail: and...@bullseye.apana.org.au | Snail: PO Box 370
and...@pcug.org.au | Belconnen ACT 2616
Web: http://www.andymac.org/ | Australia


Tim Roberts

unread,
Feb 6, 2002, 2:53:04 AM2/6/02
to
"DeepBleu" <Deep...@DeepBleu.org> wrote:
>
>Wang
>Please send the code indented next time. You know, you may know it inside
>out. But poor readers like me do not know your intentions and indenting
>makes it easier to read.

His code WAS indented, using hard tabs. I'm guessing Microsoft is
"helping" you here by having Outlook Express skip leading hard tabs. Next
time, try using a real news reader.
--
- Tim Roberts, ti...@probo.com
Providenza & Boekelheide, Inc.

Jason Orendorff

unread,
Feb 6, 2002, 12:19:27 PM2/6/02
to
J. Wang writes:
> import cgi
>
> print 'Content-type: text/html\n'
> form = cgi.FieldStorage()
> if not form:
> [...]

> elif form.has_key("filename"):
> item = form["filename"]
> if item.file:
> while 1:
> data = item.file.readline()
> fp = file('1.jpg','wb')
> if data:
> fp.write(data)
> else:
> break
> fp.close()

Every time through the loop you are completely overwriting your file.

Do this instead:

elif form.has_key("filename"):
item = form["filename"]
if item.file:

fp = file('1.jpg','wb')
while 1:
data = item.file.readline()
if data:
fp.write(data)
else:
break
fp.close()

## Jason Orendorff http://www.jorendorff.com/

Steve Holden

unread,
Feb 6, 2002, 1:32:48 PM2/6/02
to
"Tim Roberts" <ti...@probo.com> wrote in message
news:u1o16ugtmp7kn5mea...@4ax.com...

> "DeepBleu" <Deep...@DeepBleu.org> wrote:
> >
> >Wang
> >Please send the code indented next time. You know, you may know it
inside
> >out. But poor readers like me do not know your intentions and indenting
> >makes it easier to read.
>
> His code WAS indented, using hard tabs. I'm guessing Microsoft is
> "helping" you here by having Outlook Express skip leading hard tabs. Next
> time, try using a real news reader.

Given the proliferation of crappy newsreader software, sensible posters
adopt the Internet philosophy of being critical in what they produce and
liberal in what they accept.

So, normally I don't complain when someone posts tab-indented code, but if I
can't read it then someone else is going to have to reply.

Telling people to use some other piece of software is impractical: on a
group like this it isn't usually because they don't know any better, it's
for other pragmatic reasons. Next time, try using real manners <0.75 wink>.

using-outlook-express-and-not-a-bit-proud-of-it-ly y'rs - steve
--
Consulting, training, speaking: http://www.holdenweb.com/
Author, Python Web Programming: http://pydish.holdenweb.com/pwp/

"This is Python. We don't care much about theory, except where it
intersects with useful practice." Aahz Maruch on c.l.py


0 new messages