简体   繁体   中英

Display PDF files REST API

I am using RESTful API with python and flask to list all PDF files of a folder. The PDF filenames are displayed with their complete path. For example, http://127.0.0.1:5000/getPDFs shows the following

test1.pdf /home/PATH/test1.pdf
test2.pdf /home/PATH/test2.pdf

Now, I would like to add a link to all the paths and when the path is clicked, the PDF files should be displayed on the browser. If I click the /home/PATH/test1.pdf it should go to http://127.0.0.1:5000/getPDFs/test1.pdf and display the pdf file.

Any help would be appreciated.

You need to implement a route that points to the pdf you are looking for, in your case:

@app.route('/getPDFs/<path:filename>')
def download_file(filename):
    return send_from_directory(app.config['PATH'],
                               filename, as_attachment=True)

Your file should be visible by your flask app. You can find the documentation for send_from_directory here:

https://flask.palletsprojects.com/en/1.1.x/api/#flask.send_from_directory

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