【问题标题】:Display image from flask send_file (ajax response) into the image tag将烧瓶 send_file(ajax 响应)中的图像显示到图像标签中
【发布时间】:2021-01-03 09:18:42
【问题描述】:

如何显示来自烧瓶 send_file ajax 响应的图像

HTML 文件


<button class="button" id="start">Start</button>

    <script>
        //start button click event

        $('#start').click(function() {
            $.ajax({
                url: 'http://127.0.0.1:5000/capture',
                type: 'GET',
                contentType: "image/jpg",
                success: function(result) {
                    document.getElementById('frame').src = 'data:image/jpg,' + result;
                }
            });
        });

烧瓶


@app.route('/capture')
def capture_api():
    ...
    image_binary = img.read()

    return send_file(io.BytesIO(image_binary),
                     mimetype='image/jpeg',
                     as_attachment=True,
                     attachment_filename='%s.jpg' % "capture")

【问题讨论】:

    标签: python jquery ajax flask


    【解决方案1】:

    您必须将图像数据编码为 base64 并将其添加到 dom 中的图像元素中。

    我在 repl.it 中创建了一个示例应用程序:https://repl.it/@MichaelAnckaert/Flask-Playground

    这是一个示例 Flask 应用程序:

    from flask import Flask, render_template, make_response
    import base64
    app = Flask('app')
    
    @app.route('/')
    def hello_world():
      return render_template('image.html')
    
    
    @app.route('/capture')
    def capture_api():
      with open("house-thumbs-up.gif", "rb") as f:
        image_binary = f.read()
    
        response = make_response(base64.b64encode(image_binary))
        response.headers.set('Content-Type', 'image/gif')
        response.headers.set('Content-Disposition', 'attachment', filename='image.gif')
        return response
    
    app.run(host='0.0.0.0', port=8080)
    

    以及对应的HTML模板:

    <img id="image">
    
    <button class="button" id="start">Start</button>
    
    <script src="https://code.jquery.com/jquery-3.5.1.min.js"     integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0=" crossorigin="anonymous"></script>
    <script>
      let button = document.getElementById("start")
      button.addEventListener('click', () => {
        $.ajax({
            url: 'https://luminoustwincase--five-nine.repl.co/capture',
            type: 'GET',
            contentType: "image/jpg",
            success: function(result) {
              document.getElementById('image').src = 'data:image/gif;base64,' + result;
            }
        });
      });
    </script> 
    

    【讨论】:

      【解决方案2】:

      如果要使用字节来绘制图像,则应在 src 标签中添加base64

      document.getElementById('frame').src = 'data:image/jpg;base64,' + result;
      

      【讨论】:

        【解决方案3】:

        问题在于后端和前端的编码。

        在烧瓶中

        image = b64encode(image_binary).decode("utf-8")
        return jsonify({'status': True, 'image': image})
        

        html

        document.getElementById('frame').src = 'data:;base64,' + result['image'];
        

        【讨论】:

          猜你喜欢
          • 2021-06-28
          • 1970-01-01
          • 2021-11-09
          • 2023-04-07
          • 2012-01-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多