コード例 #1
0
        public HttpResponseMessage DonwloadZip([FromUri] List <int> list, [FromUri] List <int> docsType)
        {
            if ((list == null && (list.Count == 0 || list.Count > MAX_FILE_LIST_UPDALODED)) && (docsType == null && docsType.Count == 0))
            {
                return(Request.CreateResponse(HttpStatusCode.BadRequest));
            }

            var tempFolderArchive = HttpContext.Current.Server.MapPath(string.Format("~/App_Data/{0}", Guid.NewGuid()));
            var zipFileName       = string.Format("{0}.zip", tempFolderArchive);
            var acrhive           = new ArchiveManager(DataManager);
            int countFilesFound   = 0;

            foreach (int listId in list)
            {
                var descriptionList = ArchiveManager.GetFileListDescription(listId);
                if (string.IsNullOrEmpty(descriptionList))
                {
                    descriptionList = string.Format("Папка № {0}", listId);
                }
                var files = ArchiveManager.GetFilesFromList(listId);
                foreach (var docReq in files)
                {
                    if (!docsType.Any(d => d == (int)docReq.type))
                    {
                        continue;
                    }
                    using (var file = ArchiveManager.GetDocument(docReq))
                    {
                        if (file == null)
                        {
                            continue;
                        }
                        var dir = Directory.CreateDirectory(string.Format("{0}/{1}", tempFolderArchive, descriptionList.Replace("/", "_")));
                        if (dir != null)
                        {
                            using (var f = File.Open(string.Format("{0}/{1}", dir.FullName, docReq.fileName.Replace("/", "_")), FileMode.Create))
                            {
                                file.CopyTo(f);
                                countFilesFound++;
                            }
                        }
                    }
                }
            }
            if (countFilesFound == 0)
            {
                return(new HttpResponseMessage(HttpStatusCode.NotFound));
            }
            ZipFile.CreateFromDirectory(tempFolderArchive, zipFileName);
            var    result = new HttpResponseMessage(HttpStatusCode.OK);
            Stream stream = File.OpenRead(zipFileName);

            result.Content = new StreamContent(stream);

            result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
            result.Content.Headers.ContentDisposition.FileNameStar = string.Format("Архив файлов({0}).zip", DateTime.Now.ToShortDateString());
            result.Content.Headers.ContentType         = new MediaTypeHeaderValue("application/octet-stream");
            result.Content.Headers.ContentType.CharSet = "UTF-8";
            result.Content.Headers.ContentLength       = stream.Length;

            Directory.Delete(tempFolderArchive, true);
            tempArhiveFiles.Add(zipFileName);
            return(result);
        }