รูปแบบดังตัวอย่าง
โค้ดหน้า python
โค้ด: เลือกทั้งหมด
from flask import Flask, send_file,redirect,url_for,render_template
app = Flask(__name__)
@app.route('/')
def index():
pdfpath='1.pdf'
return render_template('home.html',data=pdfpath)
@app.route('/pdfshow/<path:data>', methods=['GET'])
def pdf(data):
resp = send_file(data)
resp.headers['Content-Disposition'] = "inline; filename=%s" % 'test.pdf'
resp.mimetype = 'application/pdf'
return resp
if __name__ == '__main__':
app.run(debug=True)
โค้ด: เลือกทั้งหมด
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<a href="{{url_for('pdf', data=data)}}">คลิก PDF</a>
</body>
</html>
โค้ด: เลือกทั้งหมด
$ curl -i http://127.0.0.1:5000/pdfshow/1.pdf
HTTP/1.0 200 OK
Content-Length: 60681
Content-Type: application/pdf
Last-Modified: Fri, 28 Jun 2019 04:50:15 GMT
Cache-Control: public, max-age=43200
Expires: Mon, 01 Jul 2019 20:55:42 GMT
ETag: "1561697415.794865-60681-6360362"
Content-Disposition: inline; filename=test.pdf
Server: Werkzeug/0.15.4 Python/3.6.5
Date: Mon, 01 Jul 2019 08:55:42 GMT
Warning: Binary output can mess up your terminal. Use "--output -" to tell
Warning: curl to output it to your terminal anyway, or consider "--output
Warning: <FILE>" to save to a file.
http://www.flask.pocoo.org/snippets/100/
https://www.stackoverflow.com/questions/11773348/python-flask-how-to-set-content-type
http://www.blog.luisrei.com/articles/flaskrest.html