【问题标题】:Ajax POST to flask to download a binary (cytoscape in use)Ajax POST 到烧瓶以下载二进制文件(使用中的 cytoscape)
【发布时间】:2018-04-26 14:02:57
【问题描述】:

我正在使用 cytoscape.js 创建一个可以快速下载文件的菜单。我正在使用 Ajax POST 将文件名传递回烧瓶以便下载。出于某种原因,我可以取回所有信息,但无论出于何种原因,我都无法下载文件。到目前为止,我已经尝试了两种方法。

AJAX 帖子:

{{
                    content: 'Download',
                    select: function(ele) {{
                        //this is to get the name of URI
                        var loc = window.location.pathname
                        var postData = {{
                             "element": ele.id(),
                             "source": loc
                         }}
                        $.ajax({{
                            url: '/get_file',
                            type: "POST",
                            contentType: 'application/json',
                            data: JSON.stringify(postData),
                            dataType: 'json',
                            success: function(response) {{
                                console.log("got it!")
                            }},
                            error: function(xhr) {{
                                console.log("Nope!")
                            }}
                        }})

现在对于烧瓶后端,我们有方法一,注释部分显示方法 2 和 3(在 if 循环中(if os.path.isfile(SAVE_PATH)):

@app.route('/get_file', methods=['POST'])
def get_file():
    print("This is request data: {}".format(request.data))
    requests = request.get_json()
    element = requests['element']
    source = requests['source']
    #this is where loc is retrieved from ajax post, in format of /static/{filename}.html
    for change in ['/static/', '.html']:
        if change in source:
            source = source.replace(change,"")
    print("source: {}".format(source))
    SAVE_PATH = os.path.curdir + "/results/" + source + "/" + element
    SAVE_DIRECTORY = os.path.curdir + "/results/" + source + "/"
    if os.path.isfile(SAVE_PATH):
        downloaded_file = open("{}".format(SAVE_PATH), 'rb').read()
        #res = send_from_directory(SAVE_PATH.replace("./", ""), element, attachment_filename=element, mimetype="application/octet-stream", as_attachment=True)
        #res = send_file(SAVE_PATH, as_attachment=True, attachment_filename=element, mimetype='application/octet-stream')
        #return res
        return Response(
            downloaded_file,
            mimetype="application/octet-stream",
            headers={"Content-disposition":
                     "attachment; filename={}".format(element)})
    else:
        print("failed")
    return "failed"

现在我得到了所有正确的响应,当我打印出下载的文件时,我得到了二进制输出,但无论出于何种原因,它只是没有下载。

【问题讨论】:

    标签: python ajax python-2.7 flask cytoscape.js


    【解决方案1】:

    看起来你可以在前端构建下载路径并直接请求文件,如下所示:

    location = loc.split("/");
    path = "results/" + location[location.length - 1].split(".")[0] + "/" + ele.id();
    window.location.href = path;
    

    【讨论】:

    • ty ty,过度设计它
    猜你喜欢
    • 2014-08-25
    • 1970-01-01
    • 2012-12-02
    • 1970-01-01
    • 1970-01-01
    • 2021-07-15
    • 2017-04-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多