I Love Stackoverflow
I Love Stackoverflow

Reputation: 6868

Getting error when creating zip file to download

I am trying to create zip files for users to download and this file can be any file, like an image or text file etc.

I want to create a zip file and download it on my anchor tab click event.

I am having 3 files like this in my database table:

https://my.net/storage/log.txt

https://my.net/storage/log1.txt

https://my.net/storage/log2.txt

This is my Code:

public ActionResult DownloadImagefilesAsZip()
{
    string documentUrl = repossitory.GetDocumentsUrlbyId(id);//output:https://my.net/storage/log.txt,
https://my.net/storage/log1.txt,
https://my.net/storage/log2.txt,


  if (!string.IsNullOrEmpty(documentUrl))
            {
                string[] str = documentUrl.Split(',');
                if (str.Length > 1)
                {
                    return new ZipResult(str);
                 }  
            }
    }



public class ZipResult : ActionResult
    {
        public string[] Filename1 { get; private set; }

        public ZipResult( string[] str)
        {
            Filename1 = str;
        }

        public override void ExecuteResult(ControllerContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            var response = context.HttpContext.Response;
            response.ContentType = "application/gzip";
            using (var zip = new ZipFile())
            {
                foreach (string t in Filename1)
                {
                    if (!string.IsNullOrEmpty(t))
                    {
                        zip.AddFile(t);
                    }
                }
                //zip.AddDirectory(Path);
                zip.Save(response.OutputStream);
                var cd = new ContentDisposition
                {
                    FileName = "Images.Zip",
                    Inline = false
                };
                response.Headers.Add("Content-Disposition", cd.ToString());
            }
        }
    }

Error:The given path's format is not supported on below line:

 zip.AddFile(t);

Upvotes: 1

Views: 476

Answers (1)

lbrahim
lbrahim

Reputation: 3810

Zip expects the links to your files in terms of your machine but you are giving it remote urls. So, in your case there will be 2 steps:

  1. First download the file to your server
  2. Zip them and respond to the client with it

Find a sample for this below. I have tried it and works.

public ActionResult Index()
{
    var destination = Server.MapPath("~/Downloads/144_ctrl.txt");
    var fileUrl = "http://textfiles.com/computers/144_ctrl.txt";           

    using (var web = new WebClient())
    using (var zip = new ZipFile())
    {
        web.DownloadFile(new Uri(fileUrl), destination);

        zip.AddDirectory(Server.MapPath("~/Downloads"));
        MemoryStream output = new MemoryStream();
        zip.Save(output);
        return File(output, "application/zip", "sample.zip");
    }
}

Upvotes: 1

Related Questions