Python 文件上传不起作用,但dosen';Don’我不会出错的
第一件事: -我正在使用Python2.7.7,因为cPanel已损坏。 -这是我为迎接挑战而写的博客 我想做的是,在Python 文件上传不起作用,但dosen';Don’我不会出错的,python,python-2.7,Python,Python 2.7,第一件事: -我正在使用Python2.7.7,因为cPanel已损坏。 -这是我为迎接挑战而写的博客 我想做的是,在p\u image字段中上传一个文件,并将其保存在assets/images/post\u images/中,然后将其命名为p\u title+原始文件名(p\u title是帖子的名称)。 这是我目前的代码 filedata = data['p_image'] if filedata.file: with file(data.getvalue("p_title"), '
p\u image
字段中上传一个文件,并将其保存在assets/images/post\u images/
中,然后将其命名为p\u title+原始文件名(p\u title
是帖子的名称)。
这是我目前的代码
filedata = data['p_image']
if filedata.file:
with file(data.getvalue("p_title"), 'w') as outfile:
outfile.write(filedata.file.read())
post = "Success"
#start code stuff such as Content-type html \r\n\r\n don't worry it's the file upload not the print script
print post
这是html脚本
<div class="create_post">
<form method="GET" action="post_blog.py" enctype="multipart/form-data">
<input type="text" name="p_auth" placeholder="Enter Post Authenticator" />
<h2>Post preview</h2>
<input type="file" name="p_image"><br/>
<input type="text" name="p_title" placeholder="Title your post" /><br/>
<input type="text" name="p_date" placeholder="Date" /><br/>
<h1>Inside</h1>
<p>Title, date & preview image are to be placed here!<p>
<input type="text" name="p_desc" placeholder="Post Description" /><br/>
<input type="submit" value="Post!">
</form>
</div>
这给了我这个错误
<type 'exceptions.TypeError'> Python 2.6.6: /usr/bin/python
Wed Jul 25 21:08:33 2018
A problem occurred in a Python script. Here is the sequence of function calls leading up to the error, in the order they occurred.
/home/bearncat/*removed link due to privacy*/post_blog.py in ()
39 post = str(filedata) + "\n"
40 if filedata.file:
41 with file(data.getvalue("p_image"), 'w') as outfile:
42 outfile.write(filedata.file.read())
43 post += "Success"
builtin file = <type 'file'>, data = FieldStorage(None, None, [FieldStorage('p_auth',..., ''), FieldStorage('p_desc_image_1', None, '')]), data.getvalue = <bound method FieldStorage.getvalue of FieldStor... ''), FieldStorage('p_desc_image_1', None, '')])>, outfile undefined
<type 'exceptions.TypeError'>: file() argument 1 must be encoded string without NULL bytes, not str
args = ('file() argument 1 must be encoded string without NULL bytes, not str',)
message = 'file() argument 1 must be encoded string without NULL bytes, not str'
您是否检查了数据['p_image']本身是否包含数据?如果您为post_blog.py发布代码,这会有所帮助。另外,form方法真的是get吗?如果文件数据是get,则无法传递。因此,数据[p_image]
确实包含类似于.png
的内容。我将表单更改为POST,但现在,它给了我一个类型错误。@FranreySaycon完整代码粘贴在:。HTML代码位于:。快速编辑,python代码可以工作如果文件名中没有data.getvalue()
,它可以工作。data.getvalue(“p_图像”)可能有空字符\0。这是一个非常常见的错误。@FranreySaycon如何在不损坏文件的情况下删除它们?
<type 'exceptions.TypeError'> Python 2.6.6: /usr/bin/python
Wed Jul 25 21:08:33 2018
A problem occurred in a Python script. Here is the sequence of function calls leading up to the error, in the order they occurred.
/home/bearncat/*removed link due to privacy*/post_blog.py in ()
39 post = str(filedata) + "\n"
40 if filedata.file:
41 with file(data.getvalue("p_image"), 'w') as outfile:
42 outfile.write(filedata.file.read())
43 post += "Success"
builtin file = <type 'file'>, data = FieldStorage(None, None, [FieldStorage('p_auth',..., ''), FieldStorage('p_desc_image_1', None, '')]), data.getvalue = <bound method FieldStorage.getvalue of FieldStor... ''), FieldStorage('p_desc_image_1', None, '')])>, outfile undefined
<type 'exceptions.TypeError'>: file() argument 1 must be encoded string without NULL bytes, not str
args = ('file() argument 1 must be encoded string without NULL bytes, not str',)
message = 'file() argument 1 must be encoded string without NULL bytes, not str'
<form method="POST" action="post_blog.py" enctype="multipart/form-data">
filedata = data['p_image']
p_filename = str(data.getvalue("p_title"))
p_filename += os.path.basename(filedata.filename)
if filedata.file:
with file(p_filename, 'w') as outfile:
outfile.write(filedata.file.read())
post += "Success"