flask上传不适用于VPS

时间:2016-05-10 12:27:15

标签: python html flask flask-uploads

我遇到了内部服务器错误'当我尝试从我的数字海洋帐户运行我的代码。我已尝试在本地运行代码,它工作得很好,当我在www.pythonanywhere.com上尝试它时,它运行良好,没有任何错误。

import os
from flask import Flask, render_template, request
app = Flask(__name__)
APP_ROOT = os.path.dirname(os.path.abspath(__file__))
@app.route("/")
def index():
     return render_template("upload.html")

@app.route("/upload", methods = ['POST'])
def upload():
    target = os.path.join(APP_ROOT, 'images/')
    print(target)

    if not os.path.isdir(target):
        os.mkdir(target)

    for file in request.files.getlist("file"):
        print(file)
        filename = file.filename
        destination = "/".join([target, filename])
        print(destination)
        file.save(destination)
    return render_template("complete.html")

 if __name__ == "__main__":
    app.run()

这段代码创建了文件夹,并在本地和python的任何地方完美地将文件上传到它,但我无法理解为什么它不会为我的数字海洋VPS这样做。

我用于" upload.html"的html代码是:

 <!DOCTYPE html>
 <html lang="en">
 <head>
    <meta charset="UTF-8">
    <title>Upload</title>

 </head>
 <body>
    <h1> File Uploader </h1>
    <form id-"upload-form" action="{{ url_for('upload') }}" method="POST" enctype="multipart/form-data">
        <input type="file" name="file" accept="image/*" multiple>
        <input type="submit" value="send">
    </form>

 </body>
 </html>

和我的&#34; complete.html&#34;基本上只是一个paragragh标签说&#34;它工作&#34;所以我知道它没有遇到错误..

有没有其他人遇到过类似的问题?

1 个答案:

答案 0 :(得分:0)

解决了这个问题。原来这是一个文件权限问题。要解决此问题,您必须转到要上载文件的目录并输入:

 chmod -R 777 .

现在文件上传没有任何错误