V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
woshichuanqilz
V2EX  ›  Flask

网页小白求助一个基本的 flask 网页框架

  •  
  •   woshichuanqilz · 2017-09-22 16:52:20 +08:00 · 2693 次点击
    这是一个创建于 2379 天前的主题,其中的信息可能已经有所发展或是发生改变。

    我需要做一个简单的网页

    功能和这个网页类似, http://zhcn.109876543210.com/

    选择文件---> 后端处理 ---> 处理完毕 ---> 提供下载链接。

    对界面完全没有要求, 只想求一个具有类似功能的代码的框架, 自己学习了 flask

    以下是我的代码, 现在不知道如何在处理完毕之后提供下载的链接。

    多谢各位

    # -*- coding: utf-8 -*-
    import os
    from flask import Flask, request, url_for, send_from_directory
    from werkzeug import secure_filename
    
    ALLOWED_EXTENSIONS = set(['png', 'jpg', 'jpeg', 'gif','txt'])
    
    app = Flask(__name__)
    app.config['UPLOAD_FOLDER'] = os.getcwd()
    app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024
    
    
    html = '''
        <!DOCTYPE html>
        <title>Upload File</title>
        <h1>图片上传</h1>
        <form method=post enctype=multipart/form-data>
             <input type=file name=file>
             <input type=submit value=上传>
        </form>
        <form method=post enctype=multipart/form-data>
             <input type=file name=file>
             <input type=submit value=上传>
        </form>
        '''
    
    # html = html + '<a href=r"\\DESKTOP-0EVA06J\\Origami\\test.txt" download>下载</a>'
    with open('template.html', encoding = 'utf-8') as the_file:
       html = the_file.read()
    
    
    def allowed_file(filename):
        return '.' in filename and \
               filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS
    
    
    @app.route('/uploads/<filename>')
    def uploaded_file(filename):
        return send_from_directory(app.config['UPLOAD_FOLDER'],
                                   filename)
    
    
    @app.route('/', methods=['GET', 'POST'])
    def upload_file():
        if request.method == 'POST':
            file = request.files['file']
            if file and allowed_file(file.filename):
                filename = secure_filename(file.filename)
                print('the file name will be ' + os.path.join(app.config['UPLOAD_FOLDER'], filename))
    #the file will be here.
    
                file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
                file_url = url_for('uploaded_file', filename=filename)
                # print('the file_url will be ' + file_url)
                return html + '<br><img src=' + file_url + '>'
        return html
    
    
    if __name__ == '__main__':
        # app.run(host='192.168.0.176', port=5001)
        app.run(extra_files = html)
        # app.run()
    
    
    2 条回复    2017-09-22 17:42:39 +08:00
    pipapa
        1
    pipapa  
       2017-09-22 17:12:33 +08:00
    前端利用 ajax,POST 数据,获取返回值
    gosky
        2
    gosky  
       2017-09-22 17:42:39 +08:00
    对 flask 了解有限
    post /upload
    处理,保存文件到{uuid}.data
    get /results/{uuid}.data
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5431 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 08:45 · PVG 16:45 · LAX 01:45 · JFK 04:45
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.