當Flask的server運行起來的時候乍炉,不在template中的其他文件難以通過server.py的相對路徑直接得到,因此需要直接在route中添加獲取靜態(tài)文件的路由
from flask import Flask, Response
import os
def root_dir():
return os.path.abspath(os.path.dirname(__file__))
def get_file(filename):
try:
src = os.path.join(root_dir(), filename)
print root_dir()
print src
return open(src).read()
except IOError as exc:
return str(exc)
# 可以傳參的url
@app.route('/cvs/<int:number>',methods = ['GET'])
def getcv(number):
content = get_file('cvs/cv'+str(number)+'.pdf')
return Response(content,mimetype = "application/pdf") # mimetype根據(jù)文件類型查閱即可