Asp.net mvc 创建要下载的zip文件时出错

Asp.net mvc 创建要下载的zip文件时出错,asp.net-mvc,download,dotnetzip,Asp.net Mvc,Download,Dotnetzip,我正在尝试创建zip文件供用户下载,该文件可以是任何文件,如图像或文本文件等 我想创建一个zip文件,并在我的“锚定”选项卡“单击事件”上下载它 我的数据库表中有3个类似的文件: 这是我的代码: public ActionResult DownloadImagefilesAsZip() { string documentUrl = repossitory.GetDocumentsUrlbyId(id);//output:https://my.net/storage/log.txt,

我正在尝试创建zip文件供用户下载,该文件可以是任何文件,如图像或文本文件等

我想创建一个zip文件,并在我的“锚定”选项卡“单击事件”上下载它

我的数据库表中有3个类似的文件:

这是我的代码:

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());
            }
        }
    }
错误:下一行不支持给定路径的格式:

 zip.AddFile(t);

Zip希望从您的机器的角度获得指向您的文件的链接,但您正在为其提供远程URL。因此,在您的情况下,将有两个步骤:

  • 首先将文件下载到服务器
  • 压缩它们并用它回复客户
  • 在下面找到一个示例。我试过了,效果不错

    public ActionResult Index()
    {
    var destination=Server.MapPath(“~/Downloads/144_ctrl.txt”);
    var fileUrl=”http://textfiles.com/computers/144_ctrl.txt";           
    使用(var web=new WebClient())
    使用(var zip=new ZipFile())
    {
    下载文件(新Uri(fileUrl),目的地);
    AddDirectory(Server.MapPath(“~/Downloads”);
    MemoryStream输出=新的MemoryStream();
    压缩保存(输出);
    返回文件(输出,“application/zip”、“sample.zip”);
    }
    }
    
    cting zip?这意味着什么?库是否支持将HTTPS URL传递给AddFile()?如果这是您正在使用的:它说“它应该引用文件系统中的一个文件”@DavidG:Sorry没有得到you@IanGilroy:是的,我只使用它。那么有什么解决方法吗?