Javascript 将响应与文件下载和重定向结合起来
我有一个视图,upn称之为,开始下载:Javascript 将响应与文件下载和重定向结合起来,javascript,django,django-views,django-templates,Javascript,Django,Django Views,Django Templates,我有一个视图,upn称之为,开始下载: class PrintView(TemplateView): template_name = "proj/listview.html" def get(self, request, *args, **kwargs): try: filepath = TMP.IMG() if os.path.exists(filepath):
class PrintView(TemplateView):
template_name = "proj/listview.html"
def get(self, request, *args, **kwargs):
try:
filepath = TMP.IMG()
if os.path.exists(filepath):
with open(filepath, 'rb') as fh:
response = HttpResponse(fh.read(), content_type="application/force-download")
response['Content-Disposition'] = 'inline; filename=' + os.path.basename(filepath)
messages.success(request, "image download started ...")
return response
messages.error(request, "something went wrong!")
return redirect("index")
except Exception as e:
messages.error(request, e)
return redirect("index")
在我的模板中,我使用下载按钮调用模式窗口,类似于:
<button id="dl_modal_submit" type="submit"</button>
问题是,当我点击我想要的按钮时:
$('#button').click(function() {
// close modal:
$('#modal').hide();
// download files:
window.open('the_download_url', '_blank');
// relocate:
window.location = 'the_redirect_url';
});
$('#button').click(function() {
// close modal:
$('#modal').hide();
// download files:
window.open('the_download_url', '_blank');
// relocate:
window.location = 'the_redirect_url';
});