3 Temmuz 2013 Çarşamba

Download Picture as .zip file on asp.net mvc

you must download this dll -- Ionic.Zip.dll

////////////////////////////// Then we have create or picture class //////////////////////////

public class cPicture
    {
        public int CID { get; set; }
        public int AlbumID { get; set; }
        public int DosyaID { get; set; }
        public string name { get; set; }
        public double size { get; set; }
        public string type { get; set; }
        public string url { get; set; }
        public string delete_url { get; set; }
        public string thumbnail_url { get; set; }
        public string delete_type { get; set; }
        public string Tag { get; set; }
    }

///////////////// then we are creating new class ///////////////////////////////////

public class ZipResult : ActionResult
    {
        private IEnumerable<string> _files;
        private string _fileName;

        public string FileName
        {
            get
            {
                return _fileName ?? "file.zip";
            }
            set { _fileName = value; }
        }

        public ZipResult(params string[] files)
        {
            this._files = files;
        }

        public ZipResult(IEnumerable<string> files)
        {
            this._files = files;
        }

        public override void ExecuteResult(ControllerContext context)
        {
            using (ZipFile zf = new ZipFile())
            {
                zf.AddFiles(_files, false, "");
                context.HttpContext
                    .Response.ContentType = "application/zip";
                context.HttpContext
                    .Response.AppendHeader("content-disposition", "attachment; filename=" + FileName);
                zf.Save(context.HttpContext.Response.OutputStream);
            }
        }
    }

/// Now we are creating or ActionResult for download/////////////////////
public ActionResult DownloadPicture()
        {
            
                IEnumerable<cPicture> pictures = // our cPictureList
                string basePath = Server.MapPath("~/UserFiles/");
                IEnumerable<string> pp = pictures.Select(a => Path.Combine(basePath, a.name)).ToArray();

                return new ZipResult(pp);
        }


<a href="/Home/DownloadPicture/" target="_blank">Download File</a>

Yeah That's it.

Hiç yorum yok:

Yorum Gönder