Question

I'm trying to set up a file upload web form that is processed by a python script. When I select a file and click upload, it says that no file was uploaded. The file field of the fileitem object is None. This script is running on a lighthttpd server.

The code for the script is here:

#!/usr/bin/env python

import cgi, os
import cgitb
cgitb.enable()

form = cgi.FieldStorage()
# A nested FieldStorage instance holds the file
fileitem = form['filename']
print "----"
print "filename", fileitem.filename
print "file", fileitem.file
print "----"

message = ''
if fileitem.file:
    # It's an uploaded file; count lines
    linecount = 0
    while 1:
        line = fileitem.file.readline()
        if not line: break
        linecount = linecount + 1
    message = linecount

# Test if the file was uploaded
if fileitem.filename:

   # strip leading path from file name to avoid directory traversal attacks
   fn = os.path.basename(fileitem.filename)
   open('/var/cache/lighttpd/uploads/' + fn, 'wb').write(fileitem.file.read())
   message = 'The file "' + fn + '" was uploaded successfully'

else:
  message += 'No file was uploaded'

print """\
Content-Type: text/html\n
<html><body>
<p>%s</p>
</body></html>
""" % (message,)

The html file is here:

<html>
<head>
<title>RepRap Pinter</title>
</head>
<body>
<H1>RepRap Printer</H1>
<form action="cgi-bin/start_print.py" method="post" encrypt="multipart/form-data">
<p><input type="file" name="filename" id="file"></p>
<p><input type="submit" value="Upload"></p>
</form>
</body>
</html>

And the output is:

----
filename None
file None
----
Content-Type: text/html

<html><body>
<p>No file was uploaded</p>
</body></html>

Any ideas as to why the file isn't being uploaded?

Was it helpful?

Solution

Your problem seems to be here:

<form ... encrypt="multipart/form-data">

The attribute you are looking for isn't encrypt, it's enctype. Since you are missing the correct parameter your encoding isn't multipart-formdata so file uploads are ignored.

OTHER TIPS

You are using the wrong attribute name:

<form action="cgi-bin/start_print.py" method="post" encrypt="multipart/form-data">

The correct attribute is enctype, not encrypt:

<form action="cgi-bin/start_print.py" method="post" enctype="multipart/form-data">
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top