简体   繁体   中英

Python Flask: send file from other link

How to send file from the result of the request, if i write the file to a folder it works, but it takes time to write. but what i want is directly send from request result. sorry for my bad english

 import requests

 def get(cls, date: str, date_end: str, office_in_charge: str):
        user = get_jwt_identity()
        url = "http://localhost:9090/jasperserver/rest_v2/reports/reports/interactive/DTR.pdf"
        r = requests.get(url,
                         params={
                             'username': 'jasperadmin',
                             'password': 'jasperadmin',
                             'p1': user,
                             'p2': user,
                             'date': date,
                             'date_end': date_end,
                             'office_in_charge': office_in_charge
                         }
                         , allow_redirects=True)

        return send_file(r.content, as_attachment=True, mimetype='application/pdf')

This might work for you:

 import requests, io

 def get(cls, date: str, date_end: str, office_in_charge: str):
        user = get_jwt_identity()
        url = "http://localhost:9090/jasperserver/rest_v2/reports/reports/interactive/DTR.pdf"
        r = requests.get(url,
                         params={
                             'username': 'jasperadmin',
                             'password': 'jasperadmin',
                             'p1': user,
                             'p2': user,
                             'date': date,
                             'date_end': date_end,
                             'office_in_charge': office_in_charge
                         }
                         , allow_redirects=True)

        return send_file(
                     io.BytesIO(r.content),
                     attachment_filename='report.pdf',
                     mimetype='application/pdf'
               )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM