1

我正在尝试使用 Flask 将 pdf 作为 api 响应发送。但是,我ERR_CONTENT_LENGTH_MISMATCH在 api 的客户端得到了一个。也许这是由于文件发送到客户端时流不完整造成的?身份证

from flask import Flask, send_file, make_response

app = Flask(__name__)

@app.route("/")
def get_pdf(request):
    # Set CORS headers for the preflight request
    if request.method == 'OPTIONS':
        # Allows GET requests from any origin with the Content-Type
        # header and caches preflight response for an 3600s
        headers = {
            'Access-Control-Allow-Origin': '*',
            'Access-Control-Allow-Methods': 'POST',
            'Access-Control-Allow-Headers': 'Content-Type',
            'Access-Control-Max-Age': '3600'
        }

        return ('', 204, headers)

    pdf = Pdf.open('test.pdf')
    file = io.BytesIO()
    pdf.save(file)

    response = make_response(send_file(file, download_name="project_pdf", mimetype='application/pdf', as_attachment=True))
    response.headers.set('Access-Control-Allow-Origin', '*')

    return response
4

0 回答 0