python flask没有带send_file的输出

时间:2018-06-14 12:03:43

标签: python ajax flask powerpoint sendfile

我想从jinja模板中的javascript图形创建一个png,然后使用按钮将其导入到Powerpoint中。我在视图中使用ajax作为base64字符串获取图形,并创建png。但是当我按下按钮时,没有出现powerpoint文件。

我的python代码

@blueprint.route('/download', methods=["GET","POST"])

def download():

    dataURL = request.form.get('dataURL')

    from binascii import a2b_base64
    Picture = dataURL[22:]   
    binary_picture = a2b_base64(picture)
    fd = open('picture.png', 'wb')
    fd.write(binary_picture)
    fd.close()

    from pptx import Presentation
    from pptx.util import Inches, Pt
    from io import BytesIO 
    from pptx.dml.color import RGBColor
    from pptx.enum.text import PP_ALIGN

    prs =  Presentation('empty.pptx')
    title_slide_layout = prs.slide_layouts[2]
    slide = prs.slides.add_slide(title_slide_layout)
    title = slide.shapes.title
    subtitle = slide.placeholders[1]

    title.text = "Report"
    blank_slide_layout = prs.slide_layouts[7]
    slide = prs.slides.add_slide(blank_slide_layout)
    title = slide.shapes.title
    title.text = "XXX"

    slide.shapes.add_picture("backlog.png", left=Inches(0.10), top=Inches(1.1), width=Inches(5.7), height=Inches(2.75))

    out_file = BytesIO()
    prs.save(out_file)
    out_file.seek(0)

    return send_file(out_file, attachment_filename="Report.pptx") 

我的js代码

          $(function() {
            $('button').click(function() {
                    var dataURL = canvas.toDataURL();
                $.ajax({
                    url: '{{ url_for('home_blueprint.download') }}',
                    data: { dataURL: dataURL },
                    type: 'POST',
                    success: function(response) {
                        console.log(response);
                    },
                    error: function(error) {
                        console.log(error);
                    }
                });
            });
        });

0 个答案:

没有答案