如何使用HttpClient C#下载多个文件?

时间:2019-04-24 09:58:53

标签: c#

我想使用'HttpClient'C#下载多个文件。

我已经成功完成了单个文件GET的代码,但是当客户端调用API时,我希望下载多个文件。

我研究了一些HttpClient,HttpWebResponse和WebClient。对于我的代码,我使用HttpResponseMessage我真的不知道它是哪个类别。抱歉,我不太清楚这个概念。一些解释会很好。

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

我应该在代码中添加什么或可以使用什么方法?谢谢

0 个答案:

没有答案