Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用web Api和angular 2下载大型zip文件_Angular_Asp.net Core Mvc - Fatal编程技术网

使用web Api和angular 2下载大型zip文件

使用web Api和angular 2下载大型zip文件,angular,asp.net-core-mvc,Angular,Asp.net Core Mvc,获取下载zip文件的Web API方法 此代码不起作用 请帮我解决这个问题 public HttpResponseMessage SampleDownload() { try { HttpResponseMessage result = null; var path = @"D:\sample\sample.zip"; v

获取下载zip文件的Web API方法

此代码不起作用

请帮我解决这个问题

public HttpResponseMessage SampleDownload()
            {
                try {
                    HttpResponseMessage result = null;
                    var path = @"D:\sample\sample.zip";
                    var filename = "sample.zip";
                    if (File.Exists(path))
                    {
                        result = new HttpResponseMessage(HttpStatusCode.OK);
                        var stream = new FileStream(path, FileMode.Open);
                        result.Content = new StreamContent(stream);
                        result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
                        result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
                        result.Content.Headers.ContentDisposition.FileName = filename;
                    }
                    return result;
    }
C#Web API代码:

public IActionResult GetZipFile(string filename)
{
    // It can be zip file or pdf but we need to change contentType respectively
    const string contentType ="application/zip";
    HttpContext.Response.ContentType = contentType;
    var result = new FileContentResult(System.IO.File.ReadAllBytes(@"{path_to_files}\file.zip"), contentType)
    {
         // It can be zip file or pdf but we need to change the extension respectively
        FileDownloadName = $"{filename}.zip"
    };

    return result;
}
角度代码:

public IActionResult GetZipFile(string filename)
{
    // It can be zip file or pdf but we need to change contentType respectively
    const string contentType ="application/zip";
    HttpContext.Response.ContentType = contentType;
    var result = new FileContentResult(System.IO.File.ReadAllBytes(@"{path_to_files}\file.zip"), contentType)
    {
         // It can be zip file or pdf but we need to change the extension respectively
        FileDownloadName = $"{filename}.zip"
    };

    return result;
}
要求:FileSaver(安装文件保护程序包)

从“文件保护程序”导入*作为文件保存程序

 this.http.get(url,{ responseType: ResponseContentType.Blob }).subscribe((response)=>{
             var blob = new Blob([response['_body']], {type: "application/zip"});
              FileSaver.saveAs(blob,dlData.file_name);
        }).catch((error) => {
           // Error code
        });

参考url:

您好,可能包括您接收到的错误或输出,以便深入了解代码失败的原因:)谢谢Flauntster,我让代码正常工作了。但方法不同。参考文献:“”。对客户端做了一些修改。@trinetra:将您为解决问题所做的事情作为答案发布,然后在可能的情况下接受它。这样,这个问题就不会永远处于“未回答”状态。@ChrisPratt谢谢提醒。我明天做