Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Uploading files with cgi - binary-files are truncated !? (http://www.velocityreviews.com/forums/t323394-uploading-files-with-cgi-binary-files-are-truncated.html)

dermoon 10-08-2003 10:21 PM

Uploading files with cgi - binary-files are truncated !?
 
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()


All times are GMT. The time now is 07:12 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.