简体   繁体   English

如何在 angularJs 应用程序中从服务器生成和下载 zip 文件?

[英]How can i generate and download zip file from server in angularJs application?

My code is below:我的代码如下:

    [System.Web.Mvc.HttpPost]
    public ActionResult DownloaZipFile([FromBody] int id)
    {
        var result = _service.GetDocuments(id);
        var downloadFileName = $"Report{id}.zip";
        var downloadFilePath = Server.MapPath($"~/Uploads/TempZipDownload/{downloadFileName}");

        if (System.IO.File.Exists(downloadFilePath))
        {
            System.IO.File.Delete(downloadFilePath);
        }

        var zip = ZipFile.Open(downloadFilePath, ZipArchiveMode.Create);

        foreach (var file in result)
        {
            zip.CreateEntryFromFile(Server.MapPath(Path.Combine("~/Uploads/TempImageDownload/" + file.Filename)), file.Filename);
        }

        zip.Dispose();
        return File(downloadFilePath, "application/zip", downloadFileName);
    }

AngularJs code from component:来自组件的 AngularJs 代码:

vm.downloadReport = function(id) {
            service.DownloadReport(id).then(function(response) {

                var file = new Blob([response.data],
                {
                    type: 'application/zip'
                });

                if (navigator.msSaveBlob) {
                    navigator.msSaveBlob(file);
                } else {
                    var fileUrl = URL.createObjectURL(file);
                    console.log(fileUrl);
                    var a = document.createElement('a');
                    a.href = fileUrl;
                    a.download = 'ReportDownload' + id + '.zip';
                    document.body.appendChild(a);
                    a.click();
                }
            });
        }

After all code its downloading zip file but when I am trying to open zip file its giving me error.毕竟编码它下载的 zip 文件,但是当我尝试打开 zip 文件时,它给了我错误。 Invalid zip file.无效的 zip 文件。

Please note I have used System.IO.Compression libraries to generate and download zip file.请注意,我使用 System.IO.Compression 库来生成和下载 zip 文件。

I fixed this issue by doing following:我通过执行以下操作解决了这个问题:

    [System.Web.Mvc.HttpPost]
    public ActionResult DownloaZipFile([FromBody] int id)
    {
        var result = _service.GetDocuments(id);
        var downloadFileName = $"Report{id}.zip";
        var downloadFilePath = Server.MapPath($"~/Uploads/TempZipDownload/{downloadFileName}");

        if (System.IO.File.Exists(downloadFilePath))
        {
            System.IO.File.Delete(downloadFilePath);
        }

        var zip = ZipFile.Open(downloadFilePath, ZipArchiveMode.Create);

        foreach (var file in result)
        {
            zip.CreateEntryFromFile(Server.MapPath(Path.Combine("~/Uploads/TempImageDownload/" + file.Filename)), file.Filename);
        }

        zip.Dispose();
        return Json($"/Uploads/TempZipDownload/{downloadFileName}");
    }

In the AngularJs code :在 AngularJs 代码中:

vm.downloadReport = function(id) {
            service.DownloadReport(id).then(function(response) {
                var a = document.createElement('a');
                a.href = response.data;
                a.download = 'ReportDownload';
                document.body.appendChild(a);
                a.click();
            });
        }

This will catch zipped file url in response.data and download it by javascript code.这将在 response.data 中捕获压缩文件 url 并通过 javascript 代码下载它。 Hope this will helpful to others too.希望这对其他人也有帮助。

I think you can just do window.location.href = response.data instead of creating a dummy hyperlink.我认为你可以只做window.location.href = response.data而不是创建一个虚拟超链接。 See https://developer.mozilla.org/en-US/docs/Web/API/Location for documentation.有关文档,请参阅https://developer.mozilla.org/en-US/docs/Web/API/Location

You could also reduce the whole thing to a single HTTP request by using window.location.href to visit a single action URL (eg /downloadZipFile ) which creates the ZIP file and then sends it for download as a FileResult immediately.您还可以通过使用window.location.href访问单个操作 URL(例如/downloadZipFile )将整个事情减少到单个 HTTP 请求,该 URL 创建 ZIP 文件,然后立即将其作为FileResult发送以供下载。 You may even be able to create it in-memory without saving it to disk.您甚至可以在不将其保存到磁盘的情况下在内存中创建它。 Then you don't need the separate AJAX call just to get the URL of the file.然后你不需要单独的 AJAX 调用来获取文件的 URL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM