文件未在烧瓶中上传

时间:2017-10-14 15:54:48

标签: python flask

我正在尝试使用flask将文件上传到服务器,但文件没有上传。

我是python和flask的新手。

import os
from flask import *

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, 'uploads/')

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

    for file in request.files.getlist('file'):
        print(file.filename)
        destination = '/'.join([target, file.filename])
        print(destination)
        file.save(destination)

    return render_template('successful.html')

if __name__ == '__main__':
    app.run(debug = True)

upload.html

<!DOCTYPE html>
<html>
<head>
    <title> Upload file </title>
</head>
<body>
    <form id="upload-form" action="{{ url_for('upload') }}"
    method="post" enctype="multipart/form-data">
        <input type="file" name="cssv_file" multiple>
        <input type="submit" value="Submit"/>
    </form>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

更改此行

if not os.path.join(target):

if not os.path.isdir(target):