скрипт CGI python, похоже, не получает загруженный файл из html-формы



Я пытаюсь настроить веб-форму загрузки файла, которая обрабатывается скриптом python. Когда я выбираю файл и нажимаю кнопку Загрузить, он говорит, что файл не был загружен. Поле file объекта fileitem отсутствует. Этот сценарий выполняется на сервере lighthttpd.



Код для скрипта находится здесь:



#!/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/htmln
<html><body>
<p>%s</p>
</body></html>
""" % (message,)


Html-файл находится здесь:



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


И вывод:



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

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


Есть идеи, почему файл не загружается?

1067   2  

2 ответов:

Ваша проблема, кажется, здесь:

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

Атрибут, который вы ищете, не encrypt, это enctype. Поскольку вы пропускаете правильный параметр, ваша кодировка не является multipart-formdata, поэтому загрузка файлов игнорируется.

Вы используете неправильное имя атрибута:

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

Правильный атрибут enctype, а не encrypt:

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

Comments

    Ничего не найдено.