send_from_directory函数无缘无故被多次调用

时间:2019-06-28 16:37:32

标签: python ajax api pdf flask

我正在尝试计算每次点击的下载次数,但是问题是,当一次多次调用服务时,我点击如下所示:

new_downloads: 15
127.0.0.1 - - [28/Jun/2019 17:30:46] "GET /document/down?document_id=7d94092899ae11e9b3b1f4b7e27c0e4a&document_title=03.%20Indexation%20-%20suite.pdf HTTP/1.1" 200 -
new_downloads: 16
new_downloads: 17
127.0.0.1 - - [28/Jun/2019 17:30:46] "GET /document/down?document_id=7d94092899ae11e9b3b1f4b7e27c0e4a&document_title=03.%20Indexation%20-%20suite.pdf HTTP/1.1" 200 -
127.0.0.1 - - [28/Jun/2019 17:30:47] "GET /document/down?document_id=7d94092899ae11e9b3b1f4b7e27c0e4a&document_title=03.%20Indexation%20-%20suite.pdf HTTP/1.1" 206 -
new_downloads: 18
new_downloads: 18
new_downloads: 18
new_downloads: 18
127.0.0.1 - - [28/Jun/2019 17:30:48] "GET /document/down?document_id=7d94092899ae11e9b3b1f4b7e27c0e4a&document_title=03.%20Indexation%20-%20suite.pdf HTTP/1.1" 206 -
127.0.0.1 - - [28/Jun/2019 17:30:48] "GET /document/down?document_id=7d94092899ae11e9b3b1f4b7e27c0e4a&document_title=03.%20Indexation%20-%20suite.pdf HTTP/1.1" 206 -
127.0.0.1 - - [28/Jun/2019 17:30:48] "GET /document/down?document_id=7d94092899ae11e9b3b1f4b7e27c0e4a&document_title=03.%20Indexation%20-%20suite.pdf HTTP/1.1" 206 -
127.0.0.1 - - [28/Jun/2019 17:30:48] "GET /document/down?document_id=7d94092899ae11e9b3b1f4b7e27c0e4a&document_title=03.%20Indexation%20-%20suite.pdf HTTP/1.1" 206 -

这是我在烧瓶中的功能:

@app.route('/document/down')
def down_document():
    executed = False
    document_id = request.args.get('document_id')
    document_title = request.args.get('document_title')
    directory = os.path.dirname(app.instance_path) + "\\corpus" + "\\" + document_id

    # resp = make_response(send_file(path, as_attachment=True))
    resp = make_response(send_from_directory(directory=directory, filename=document_title))
    if resp:
        executed = True

    if executed:
        old_downloads = Helper.get_num_downloads(document_id)
        new_downloads = old_downloads + 1
        print("new_downloads: " + str(new_downloads))
        Helper.refresh_downloads(document_id=document_id, new_downloads=new_downloads)
        if request.args.get('username'):
            Helper.add_relationship_user_download_doc(username=request.args.get('username'), document_id=document_id)

    resp.headers['Access-Control-Allow-Origin'] = '*'
    resp.headers['Content-Type'] = 'application/force-download'
    resp.headers['Content-type'] = 'application/pdf'
    resp.headers['Content-Description'] = 'File Download'
    resp.headers['Content-Disposition'] = 'attachment'
    resp.headers['Content-Transfer-Encoding'] = 'binary'
    resp.headers['Expires'] = 'must-revalidate, post-check=0, pre-check=0'
    resp.headers['Pragma'] = 'public'
    return resp

当我删除函数send_from_directory时,该api被调用一次,但是我添加一个api将被多次调用。

这是我的ajax

$.ajax({
                url: "http://127.0.0.1:5000/document/down",
                type: "GET",
                crossDomain: true,
                dataType: 'binary',
                data: {
                    "document_id": document_id,
                    "document_title": filename,
                },
                success: function (response) {
                    console.log("success download");
                    Swal.close();
                },
                failure: function (request) {
                    alert(request);
                },
                error: function (jqXHR, exception) {
                    console.log("error download");
                    let msg = '';
                    if (jqXHR.status === 0) {
                        msg = 'Not connect.\n Verify Network.';
                    } else if (jqXHR.status === 404) {
                        msg = 'Requested page not found. [404]';
                    } else if (jqXHR.status === 500) {
                        msg = 'Internal Server Error [500].';
                    } else if (exception === 'parsererror') {
                        msg = 'Requested JSON parse failed.';
                    } else if (exception === 'timeout') {
                        msg = 'Time out error.';
                    } else if (exception === 'abort') {
                        msg = 'Ajax request aborted.';
                    } else {
                        msg = 'Uncaught Error.\n' + jqXHR.responseText;
                    }
                    alert(msg)
                }
            });

0 个答案:

没有答案