1

I want to download multiple files using 'HttpClient' C#.

I had successfully done the code for single file GET but I want multiple files download when client side call the API.

I research for some HttpClient, HttpWebResponse and WebClient. For my codes I uses HttpResponseMessage I don't really know which category is it. Sorry I'm not really clear about the concept. Some explanation would be great.

namespace WebAPIASPNET.Controllers
{
    public class EbookController : ApiController
    {
        string bookPath_Pdf = @"D:\VisualStudio\randomfile.pdf";
        string bookPath_xls = @"D:\VisualStudio\randomfile.xls";
        string bookPath_doc = @"D:\VisualStudio\randomfile.docx";
        string bookPath_zip = @"D:\VisualStudio\randomfile.zip";

        public IHttpActionResult Get(string format)
        {
            string reqBook = format.ToLower() == "pdf" ? bookPath_Pdf : (format.ToLower() == "xls" ? bookPath_xls : (format.ToLower() == "doc" ? bookPath_doc : bookPath_zip));
            string bookName = "sample." + format.ToLower();

            //converting Pdf file into bytes array  
            var dataBytes = File.ReadAllBytes(reqBook);
            //adding bytes to memory stream   
            var dataStream = new MemoryStream(dataBytes);
            return new eBookResult(dataStream, Request, bookName);
        }

        [HttpGet]
        [Route("GetIT/{format}")]
        public IHttpActionResult GetbookFor(string format)
        {
            string reqBook = format.ToLower() == "pdf" ? bookPath_Pdf : (format.ToLower() == "xls" ? bookPath_xls : (format.ToLower() == "doc" ? bookPath_doc : bookPath_zip));
            string bookName = "sample." + format.ToLower();

            //converting Pdf file into bytes array  
            var dataBytes = File.ReadAllBytes(reqBook);
            //adding bytes to memory stream   
            var dataStream = new MemoryStream(dataBytes);
            return new eBookResult(dataStream, Request, bookName);
        }
        [HttpGet]
        [Route("GetBookForHRM/{format}")]
        public HttpResponseMessage GetBookForHRM(string format)
        {
            string reqBook = format.ToLower() == "pdf" ? bookPath_Pdf : (format.ToLower() == "xls" ? bookPath_xls : (format.ToLower() == "doc" ? bookPath_doc : bookPath_zip));
            string bookName = "sample." + format.ToLower();
            //converting Pdf file into bytes array  
            var dataBytes = File.ReadAllBytes(reqBook);
            //adding bytes to memory stream   
            var dataStream = new MemoryStream(dataBytes);

            HttpResponseMessage httpResponseMessage = Request.CreateResponse(HttpStatusCode.OK);
            httpResponseMessage.Content = new StreamContent(dataStream);
            httpResponseMessage.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment");
            httpResponseMessage.Content.Headers.ContentDisposition.FileName = bookName;
            httpResponseMessage.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/octet-stream");

            return httpResponseMessage;
        }
    }

    public class eBookResult : IHttpActionResult
        {
            MemoryStream bookStuff;
            string PdfFileName;
            HttpRequestMessage httpRequestMessage;
            HttpResponseMessage httpResponseMessage;
            public eBookResult(MemoryStream data, HttpRequestMessage request, string filename)
            {
                bookStuff = data;
                httpRequestMessage = request;
                PdfFileName = filename;
            }
            public System.Threading.Tasks.Task<HttpResponseMessage> ExecuteAsync(System.Threading.CancellationToken cancellationToken)
            {
                httpResponseMessage = httpRequestMessage.CreateResponse(HttpStatusCode.OK);
                httpResponseMessage.Content = new StreamContent(bookStuff);
                //httpResponseMessage.Content = new ByteArrayContent(bookStuff.ToArray());  
                httpResponseMessage.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment");
                httpResponseMessage.Content.Headers.ContentDisposition.FileName = PdfFileName;
                httpResponseMessage.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/octet-stream");

                return System.Threading.Tasks.Task.FromResult(httpResponseMessage);
            }
        }
    }                                          

What should I add into the codes or what method can I use? Thanks

Cheeseburger
  • 185
  • 3
  • 12
  • How about [adding the files to a .zip file and returning that](https://stackoverflow.com/questions/26574903/how-to-return-multiple-files-from-asp-net-web-api)? – stuartd Apr 24 '19 at 10:01
  • 1
    Possible duplicate of [Download multiple files in parallel using c#](https://stackoverflow.com/questions/22747645/download-multiple-files-in-parallel-using-c-sharp) – Gokul Maha Apr 24 '19 at 10:08
  • @GokulMaha but I wanted to use HttpClient not WebClient ... – Cheeseburger Apr 25 '19 at 01:21
  • So? Then use HttpClient. – mason Apr 25 '19 at 01:28
  • Are you sure that a client cannot load files one by one using the current API? Loading [several files](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition) looks like not REST specific. – vladimir Apr 25 '19 at 01:35

0 Answers0