临时使用python实现一个文件下载链接,代码写的比较简易,贴出核心demo供参考。
Django
from django.shortcuts import HttpResponse
def download_file(request):
file = open('xx.file', 'rb')
response = HttpResponse(file)
response['Content-Type'] = 'application/octet-stream'
response['Content-Disposition'] = 'attachment;filename="xx.file"'
return response
Flask
from flask import send_file
def download_file():
return send_file('xx.file',
as_attachment=True,
attachment_filename='xx.file')
没有贴出路由,实际访问路由地址即可下载指定文件。