html - Django 重命名下载文件

标签 html django

在服务器中,文件的名称是“abc.dat”,但我想当人们下载它时将文件重命名为“xyz.doc”。我的服务器基于Django。

最佳答案

通过在响应中设置文件来使用 Django 提供文件

然后,您可以强制重命名附件的 filename= 属性。 这实际上可以是任何东西。

它是这样完成的:

response = HttpResponse("read your file here ie. abc.dat", mimetype="application/msword")
response['Content-Disposition'] = 'attachment; filename=%s' % ("xyz.doc")
return response

希望对你有帮助!

关于html - Django 重命名下载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16209070/

相关文章:

html - 如何制作不带Flash的基本音频控件?

javascript - Fabric.js 对象在以编程方式更改属性后消失

python - 需要字段的 django rest 框架错误

python - Django 模型 manytomany 字段计数出现

javascript - 多个 CSS 动画 : How to avoid re-triggering one of them?

javascript - 可以在没有 API 的情况下在 iframe 中打开 instagram

html - 如何解决介绍页高度

Python Social Auth NotAllowedToDisconnect at/disconnect/facebook/1/

python - Django Web 应用程序中的 SMTP 问题

python - Django select_lated 查询不会将所有值返回到模板