1

Get Web-API method to download zip file

This code is not working

Please help me out on this.

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;
    }
4
  • 1
    Hi, perhaps include what error or output you are receiving, to give some insight into why the code is failing? :) Commented Nov 9, 2017 at 7:23
  • Thanks Flauntster , I got the code working.but with a different approach. Reference: " stackoverflow.com/questions/41383338/… ". Did some modification for client-side. Commented Nov 9, 2017 at 9:32
  • @trinetra: Post what you did to solve the issue as answer and then accept it when you can. That way this question doesn't remain perpetually in the "unanswered" status. Commented Nov 9, 2017 at 14:18
  • @ChrisPratt Thanks for reminding. Will do it tomorrow Commented Nov 9, 2017 at 15:35

1 Answer 1

1

C# Web API Code:

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;
}

Angular Code:

Requirement: FileSaver ( Install file saver package)

import * as FileSaver from 'file-saver';

 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
        });

Reference url : How to download a ZipFile from a dotnet core webapi?

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.