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()
>
>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.
>
> 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
{...}
> 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
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.
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/
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