Python 使用 Flask 上传文件夹和文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19898283/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 14:55:50  来源:igfitidea点击:

Folder and files upload with Flask

pythonfile-uploadflask

提问by SamuraiT

I can upload a File with flask by following Uploading Files:

我可以通过以下上传文件上传带有烧瓶的文件

  1. A <form>tag is marked with enctype=multipart/form-dataand an <input type=file>is placed in that form.
  2. The application accesses the file from the files dictionary on the request object.
  3. Use the save()method of the file to save the file permanently somewhere on the filesystem.
  1. <form>标签标有enctype=multipart/form-data和一个<input type=file>被放置在该形式。
  2. 应用程序从请求对象的文件字典中访问文件。
  3. 使用save()文件的方法将文件永久保存在文件系统的某处。

But I don't know how to upload folder or some files. I searched, and I found Uploading multiple files with Flask.

但我不知道如何上传文件夹或一些文件。我搜索了一下,发现使用 Flask 上传多个文件

However, still I don't know how to upload a folder and files that belong to the folder.

但是,我仍然不知道如何上传属于该文件夹的文件夹和文件。

Could you please tell how?

你能告诉我怎么做吗?



Directory tree I am working on:

我正在处理的目录树:

.
├── manage.py
├── templates
│?? ├── file_upload.html
│?? └── hello.html
└── uploads
    ├── BX6dKK7CUAAakzh.jpg
    └── sample.txt

Source code of uploading file:

上传文件的源代码:

from flask import Flask,abort,render_template,request,redirect,url_for
from werkzeug import secure_filename
import os
app = Flask(__name__)
UPLOAD_FOLDER = './uploads'
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER 
@app.route('/')
def index():
    return redirect(url_for('hello'))

@app.route('/hello/')
@app.route('/hello/<name>')
def hello(name = None):
    return render_template('hello.html',name=name)

@app.route('/upload/',methods = ['GET','POST'])
def upload_file():
    if request.method =='POST':
        file = request.files['file']
        if file:
            filename = secure_filename(file.filename)
            file.save(os.path.join(app.config['UPLOAD_FOLDER'],filename))
            return hello()
    return render_template('file_upload.html')


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

template for file uploading(manage.py):

文件上传模板(manage.py):

<!doctype html>
<title>Upload new File</title>
<h1>Upload new File</h1>
<form action='' method="POST" enctype="multipart/form-data">
    <p><input type='file' name='file[]' multiple=''>
    <input type='submit' value='upload'>
    </p>

</form>

回答by GHETTO.CHiLD

the issue here is that flask's app.configisn't relative to itself, it's absolute. so when you put:

这里的问题是烧瓶app.config与自身无关,它是绝对的。所以当你把:

UPLOAD_FOLDER = './uploads' 

flask doesn't find this directory and returns a 500error. if you changed it to:

flask 找不到此目录并返回500错误。如果您将其更改为:

UPLOAD_FOLDER = '/tmp'  

and then uploaded your file and navigated to the /tmp/ directory you would see it.

然后上传您的文件并导航到您会看到的 /tmp/ 目录。

you will need to edit your path to the proper directory for the file to be uploaded properly.

您需要编辑正确目录的路径才能正确上传文件。

回答by Satyaki Sanyal

file = request.files['file']

change it to

将其更改为

file = request.files['file[]']