使用Flask上传文件夹和文件

编程入门 行业动态 更新时间:2024-10-22 13:46:10
本文介绍了使用Flask上传文件夹和文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我可以通过遵循上传文件

来上传带有烧瓶的文件:

  • <form>标签用enctype=multipart/form-data标记,并以该形式放置<input type=file>.
  • 应用程序从请求对象上的文件字典访问文件.
  • 使用文件的save()方法将文件永久保存在文件系统中的某个位置.
  • 但是我不知道如何上传文件夹或某些文件.我搜索了一下,发现使用Flask上载多个文件.

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

    请您告诉我如何?

    我正在研究的目录树:

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

    上传文件的源代码:

    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)

    文件上传模板(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>

    解决方案

    file = request.files['file']

    将其更改为

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

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

  • A <form> tag is marked with enctype=multipart/form-data and an <input type=file> is placed in that form.
  • The application accesses the file from the files dictionary on the request object.
  • Use the save() method of the file to save the file permanently somewhere on the filesystem.
  • But I don't know how to upload folder or some files. I searched, and I found Uploading multiple files with 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):

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

    解决方案

    file = request.files['file']

    change it to

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

    更多推荐

    使用Flask上传文件夹和文件

    本文发布于:2023-10-11 22:47:03,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1483055.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:文件夹   上传   文件   Flask

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!