代码改变世界

Asp.Net Web API 2 学习系列目录整理

2014-05-10 16:26  音乐让我说  阅读(276)  评论(0编辑  收藏  举报

 

1. aehyok - Asp.Net Web API 2 系列

2. ASP.NET Core WebApi 使用 Swagger 生成 API 说明文档

3. Web API 如何提供 Zip 供客户端下载?

代码:

        [HttpPost]
        public HttpResponseMessage DownloadMultipleFile(ImagesRequest data)
        {
            var ids = new List<int>();
            List<Image> images = new List<Image>();
            foreach (var obj in data)
            {
                ids.Add(obj.Id);
                images.Add(ImageList.GetImage(obj.Id));
            }
            var temp = AppDomain.CurrentDomain.BaseDirectory + "/temp";
            using (Ionic.Zip.ZipFile zip = new Ionic.Zip.ZipFile())
            {
                //zip.word = word;
                foreach (var img in images)
                {
                    string fullPath = $"{AppDomain.CurrentDomain.BaseDirectory}\\Content\\images\\{img.Id}.jpg";
                    //System.IO.File.Copy(fullPath, Path.Combine(temp, Path.GetFileName(fullPath)));
                    zip.AddFile(fullPath);
                }
                zip.Save(temp + "\\" +"ZipDownload.zip");
                var pushStreamContent = new System.Net.Http.PushStreamContent((stream, content, context) =>
                {
                    zip.Save(stream);
                    stream.Close(); // After save we close the stream to signal that we are done writing.
                }, "application/zip");

                return new HttpResponseMessage(HttpStatusCode.OK) { Content = pushStreamContent };
            }
        }

 

4.

谢谢浏览!