使用angular-js和Django下载Zip文件 [英] Downloading Zip file using angular-js and django

查看:65
本文介绍了使用angular-js和Django下载Zip文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从Django视图中下载一个Zip文件,但是我正在通过post请求从我的angularjs服务中调用views函数,该视图返回一个zip文件,但未下载它.谁能告诉我我的angularjs服务中.success函数的内容是什么,以便下载zip文件?

I am trying to download a Zip File from my Django views.But I am calling the views function from my angularjs service by post request.The view is returning a zip file but it is not being downloaded. Can anyone please tell me what are the contents of the .success function in my angularjs service such that the zip file will be downloaded?

推荐答案

此处发布的答案对我不起作用.这最终为我工作了:

The answer posted here didn't work for me. Here's what did end up working for me:

在您的JavaScript中:

In your javascript:

      $http({
                url: "/serveZip",
                method: 'POST',
                responseType: 'arraybuffer'
            }).then(function success(response){
                    var a = document.createElement('a');
                    var blob = new Blob([response.data], {'type':"application/zip"});
                    a.href = URL.createObjectURL(blob);
                    a.download = "myZip.zip";
                    a.click();
                    });

在您的views.py中:

In your views.py:

def serveZip(request):
    zipPath = "path/to/myZip.zip"
    servableZip = open(zipPath,'rb')
    response = HttpResponse(servableZip, content_type='application/zip') 
    response['Content-Disposition'] = 'attachment; filename="myZip.zip"'
    return response

在chrome中对我有用,没有在其他地方尝试过,但是如果在其他浏览器中不起作用,则可以尝试使用application/octet-stream而不是application/zip.

That worked for me in chrome, haven't tried elsewhere but you can try application/octet-stream instead of application/zip if its not working in other browsers.

这篇关于使用angular-js和Django下载Zip文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆