前端 test.html
<!DOCTYPE html>
<html>
<head>
<title>Download Example</title>
</head>
<body>
<h2>Click the button to download a file:</h2>
<a href="{% url 'download' id='13' %}" class="btn btn-primary">Download File</a>
<a href="{% url 'download' id=i.id %}"><button class="btn btn-success">下载语料</button></a>
<a href="/download/{{ i.id }}"><button class="btn btn-success">下载语料</button></a>
</body>
</html>
后端
def download_yuliao(request,id):
print('here3', os.getcwd())
filename = DB_yuliao.objects.filter(id=id)[0].filename
file_path = os.path.join(settings.YULIAO_ROOT, filename)
file = open(file_path, 'rb') # 打开文件
response = HttpResponse(FileWrapper(file), content_type='application/octet-stream')
response['Content-Disposition'] = 'attachment; filename="{}"'.format(filename).encode('utf-8', 'ISO-8859-1') # 为汉字时也支持
return response
url
path('download/<str:id>/', download, name='download'),
path('test/',test)