前端
<template slot-scope="scope">
<el-link :href="'/download_yuliao/'+scope.row.id">
<el-button type="success" icon="el-icon-download">下载语料</el-button>
</el-link>
</template>
路由
path('download_yuliao/<str:id>',download_yuliao,name='download_yuliao'), # 下载预料
后端
def download_yuliao(request,id):
filename = DB_yuliao.objects.filter(id=id)[0].filename
file_path = os.path.join(settings.YULIAO_ROOT, filename)
print(file_path)
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