Problem while uploading a binary file using cgi module 
Author Message
 Problem while uploading a binary file using cgi module
I test the script from
http://www.*-*-*.com/

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()



Fri, 23 Jul 2004 10:49:59 GMT  
 Problem while uploading a binary file using cgi module


Quote:

>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.



Fri, 23 Jul 2004 21:22:55 GMT  
 Problem while uploading a binary file using cgi module
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.

Quote:

> 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


Fri, 23 Jul 2004 22:27:23 GMT  
 Problem while uploading a binary file using cgi module

{...}

Quote:
> 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..."


Web:    http://www.andymac.org/        |        Australia



Sat, 24 Jul 2004 20:26:35 GMT  
 Problem while uploading a binary file using cgi module

Quote:

>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.
--

  Providenza & Boekelheide, Inc.


Sun, 25 Jul 2004 15:53:04 GMT  
 Problem while uploading a binary file using cgi module

Quote:
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/



Mon, 26 Jul 2004 01:19:27 GMT  
 Problem while uploading a binary file using cgi module

Quote:

> >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



Mon, 26 Jul 2004 02:32:48 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Problems with uploading binary files using cgi module

2. Special Problem and Solution regarding: CGI Binary File Uploading on Windows

3. CGI POST binary file upload?

4. uploading binary files via cgi

5. File Upload Problems with binary files (ASCII works ok)

6. upload file using cgi on Win2k IIS

7. Problem with CGI file upload

8. cgi binary upload

9. cgi.tcl / binary upload / filesize increased

10. cgi.tcl -- binary uploads?

11. Using c-extention module in CGI script, LD PATH problem

12. CGI and file upload.

 

 
Powered by phpBB® Forum Software