Python 下载文件并在响应中重定向
我想做出一个既重定向到特定url又不加载文件的响应。要下载我使用的文件,请执行以下操作:Python 下载文件并在响应中重定向,python,django,Python,Django,我想做出一个既重定向到特定url又不加载文件的响应。要下载我使用的文件,请执行以下操作: content = "Example content" filename = "example-file-name". response = HttpResponse(content=content, content_type='text/plain') response['Content-Disposi
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
要重定向到url,请执行以下操作:
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!'); });
不幸的是,这是不可能的,一次只能返回一个http响应。不过,您可以通过另一种方式来完成,重定向然后下载文件或使用AJAX。