python - 下载文件并在响应中重定向
问题描述
我想做出一个既重定向到某个 url 又下载文件的响应。要下载我使用的文件:
content = "Example content"
filename = "example-file-name".
response = HttpResponse(content=content,
content_type='text/plain')
response['Content-Disposition'] = 'attachment; filename={0}'.format(filename)
return response
重定向到网址:
response = HttpResponseRedirect(redirect_to=example_url)
有没有办法在一个单一的回应中做出这两件事?
解决方案
使用 AJAX 可能是一种解决方案,通过 AJAX 下载文件,然后在下载完成后启动重定向。
使用 JQuery 或相关插件可以轻松下载 AJAX,发帖供参考。
重定向可以在客户端成功实现,如下所示。
$.fileDownload('some/file.pdf')
.done(function () { window.location.href = 'REDIRECT_URL'; })
.fail(function () { alert('File download failed!'); });
推荐阅读
- scala - 在Scala / Spark中将一列Array of Array转换为Array类型
- c++ - Boost predef/os/bsd.h 具有递归包含
- java - 具有不同名称的同一项目给出错误:无法找到或加载主类
- c# - 为什么 ZipFile.CreateFromDirectory 在这里拒绝访问?
- java - 设置重复警报
- python-asyncio - Asyncio 致命错误:protocol.data_received() 调用失败
- bash - !cat 的输出未显示在 jupiter 笔记本中
- syntax - 使用 Case 语句的 JK 触发器的 VHDL 程序
- php - 缺少路由 ajax 帖子所需的参数
- javascript - Angularjs 请求 URL: unsafe:javascript:void(0); 对于 ng-click