码迷,mamicode.com
首页 > Web开发 > 详细

文件上传

时间:2018-10-04 10:47:39      阅读:239      评论:0      收藏:0      [点我收藏+]

标签:cto   orm   技术分享   bug   hello   from   upload   direct   tor   

from flask import Flask,request,render_template
#验证文件安全
from werkzeug.utils import secure_filename
#获取服务器的图片
from flask import send_from_directory
import os

app = Flask(__name__)

UPLOAD_PATH = os.path.join(os.path.dirname(__file__),image)


@app.route(/)
def hello_world():
    return hello world

#上传文件api
@app.route(/upload/,methods=[GET,"POST"])
def upload():
    if request.method == "GET":
        return render_template(upload.html)
    else:
        desc = request.form.get(desc)
        avater = request.files.get(avater)
        import uuid
        filename_split = avater.filename.split(.)
        filename_uuid = {0}{1}{2}.format(uuid.uuid1(),".",filename_split[1])
        filename = secure_filename(filename_uuid)
        avater.save(os.path.join(UPLOAD_PATH,filename))
        print(desc)
        return 上传成功

#获取图片api
@app.route(/image/<uuid:filename>)
def get_image(filename):
    return send_from_directory(UPLOAD_PATH,filename)

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

技术分享图片

 

文件上传

标签:cto   orm   技术分享   bug   hello   from   upload   direct   tor   

原文地址:https://www.cnblogs.com/wuheng-123/p/9741111.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!