文件上传无效,但不会出错

时间:2018-07-26 01:37:44

标签: python python-2.7

第一件事:  -我使用的是Python 2.7.7,因为cPanel损坏了。  -这是我要挑战的博客内容

我要做的是,在字段p_image中上载一个文件,并将其保存在assets/images/post_images/中,并将其命名为p_title + original file namep_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>

我很困惑为什么它不会将文件复制到我的目录中。因为它永远不会产生错误,所以它根本无法保存。

编辑:

filedata = data['p_image']
post = str(filedata) + "\n"
    if filedata.file:
        with file(data.getvalue("p_image"), 'w') as outfile:
            outfile.write(filedata.file.read())
            post += "Success"

这给了我这个错误,

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

我也确实将html更新为:

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

0 个答案:

没有答案