私は実際に約50の関連ページを検索しましたが、私の問題に似た問題は見られませんでした。送信ボタンを押すと、スクリプトが呼び出されますが、スクリプトは空のページを返し、ファイルがアップロードされていないことがわかります。コードに入力エラーはありません。何度かチェックしましたが、プロジェクトでこのコードを実行する必要があります。何が問題なのでしょう?私はubuntuの下でapacheを実行しています、そして私のコードは次のとおりです:
htmlコード:
<html><body>
<form enctype="multipart/form-data" action="save_file.py" method="post">
<p>File: <input type="file" name="file"></p>
<p><input type="submit" value="Upload"></p>
</form>
</body></html>
Pythonコード:
#!/usr/bin/env python
import cgi, os
import cgitb; cgitb.enable()
try: #windows needs stdio set for binary mode
import msvcrt
msvcrt.setmode (0, os.O_BINARY)
msvcrt.setmode (1, os.O_BINARY)
except ImportError:
pass
form = cgi.FieldStorage()
#nested FieldStorage instance holds the file
fileitem = form['file']
#if file is uploaded
if fileitem.filename:
#strip leading path from filename to avoid directory based attacks
fn = os.path.basename(fileitem.filename)
open('/files' + 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,)