Tôi vẫn chưa rõ khi nào sử dụng send_file
và khi send_from_directory
.Bình - Khi nào sử dụng send_file/send_from_directory
Tôi có một tệp trong thư mục được bảo vệ. Tôi có thể truy cập tập tin từ cả hai phương pháp. Vì vậy, có lẽ ai đó có một ví dụ làm rõ chức năng nào nên được sử dụng.
@app.route('/sfile/<id>')
def sfile(id=None):
try:
return send_file('protected/'+id, attachment_filename='python.jpg')
except:
return('')
@app.route('/sdir/<path:filename>')
def sdir(filename):
try:
return send_from_directory(
os.path.join(app.instance_path, ''),
filename
)
except:
return ''
cảm ơn rất nhiều vì lời giải thích – Alex