Uploading files with cgi - binary-files are truncated !?

Discussion in 'Python' started by dermoon, Oct 8, 2003.

  1. dermoon

    dermoon Guest

    Made this simple cgi-upload script that uses fieldstorage from a
    web-form. ASCII-based files works fine, but if i try to upload
    binary-files (word-documents, images) the files seems to be truncated
    - e.g. an image-file that contains 3398 bytes is uploaded with only
    425 bytes. Am I doing something wrong here?

    Here is the code in full:

    <!-- The form-->
    <FORM METHOD="POST" ACTION="cgi-bin/upload.py"
    enctype="multipart/form-data">
    <INPUT TYPE=FILE NAME="filename" size=40>
    <INPUT TYPE="SUBMIT" VALUE="Upload">
    </FORM>

    #upload.py
    print "Content type: text/html"
    print
    import cgi,sys,traceback,os


    def stripPath(fpname):
    #strip off leading path and drive stuff from dos/unix/mac files
    import string
    fname = fpname
    for delim in (':','/','\\'):
    while (1):
    p1 = string.find(fname,delim)
    if (p1<0): break
    fname = fname[p1+1:]
    return(fname)

    try:
    form=cgi.FieldStorage()
    if form.has_key("filename"):
    fileitem=form["filename"]

    remoteFilename=fileitem.filename

    filename = stripPath(remoteFilename)

    filedata=fileitem.value
    print len(filedata)

    localFilename = os.path.join('../', filename)
    fstrm = open(localFilename, 'wb')
    fstrm.write(filedata)
    fstrm.close()

    else:
    print 'The form is empty!'
    except:
    sys.stderr = sys.stdout
    traceback.print_exc()
     
    dermoon, Oct 8, 2003
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    27,666
  2. George Jackson
    Replies:
    1
    Views:
    221
    George Jackson
    Jan 11, 2006
  3. J. Muenchbourg

    String or binary data would be truncated error

    J. Muenchbourg, Sep 18, 2003, in forum: ASP General
    Replies:
    1
    Views:
    187
    Ray at
    Sep 18, 2003
  4. Lord Merlin
    Replies:
    8
    Views:
    431
    Lord Merlin
    Apr 21, 2004
  5. Replies:
    3
    Views:
    452
    Ted Zlatanov
    Aug 10, 2006
Loading...

Share This Page