python code example for uploading files based on flask

不言
Release: 2018-11-15 14:31:53
forward
3186 people have browsed it

The content of this article is about the code example of python uploading files based on flask. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Upload files

Achieve the goal: How to upload files to the server (save in the specified folder)

# 需要建立当前工作环境下的/static/face/目录,文件会保存在此目录下
import os
from flask import Flask,render_template,request
import uuid

app = Flask(__name__)

@app.route('/upload/',methods=['GET','POST'])
def upload():
    if request.method == 'POST':
        # 获取到用户上传的文件对象
        f = request.files['faceImg']
        print(f.filename)
        # 获取当前项目所在目录位置;
        basepath  = os.path.dirname(__file__)
        print(basepath)
        # 拼接路径, 存储文件到static/face/xxxx
        filename = os.path.join(basepath, 'static/face', f.filename)
        f.save(filename)
        return render_template('demo/upload.html', message="上传成功")
    else:
        return render_template('demo/upload.html')

app.run()
Copy after login

python code example for uploading files based on flask

python code example for uploading files based on flask

The above is the detailed content of python code example for uploading files based on flask. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template