【问题标题】:ZIP all files and then download [closed]压缩所有文件,然后下载[关闭]
【发布时间】:2016-05-10 00:29:00
【问题描述】:

首先我想将一个文件夹中的所有文件转换为 .zip,然后使用 API 和 C# 下载这个压缩文件夹。

想从客户端访问这些文件。客户端我正在使用 AngularJS 并想从服务器下载文件。我提出了不同的逻辑,但没有用。

【问题讨论】:

  • 你想通过网页下载吗?从 FTP 服务器?只需移动本地硬盘上的文件?您已经尝试过哪些 C# 代码?所有这些都将帮助我们更好地帮助您。 :)
  • 那就去做吧!好的,我并不是要粗鲁,您只需要开始编写一些代码,当您遇到困难时,我们会帮助您。这是一个类似的已解决问题,可以为您提供有关如何进行的想法:stackoverflow.com/questions/2670263/…
  • 想从客户端访问这些文件。

标签: c# file api download


【解决方案1】:

试试这个

 public string MergeFiles(string folder)
    {
        using (ZipFile zip = new ZipFile(folder))
        {
            string[] fileEntries = Directory.GetFiles(folder);
            foreach (string f in fileEntries)
            {
                string path = Path.GetDirectoryName(f.Substring(folder.Length));
                    zip.AddFile(f, path);
            }
            zip.Save(folder + "\\files.zip");
        }
        return folder+"\\files.zip";
    }

【讨论】:

  • 我可以压缩所有文件,但请告诉我将这些文件发送到客户端的方式。客户正在使用 AngularJS,因此,人们可以下载这些压缩文件。
  • 您可以使用 Response.Content.Headers.Add("content-disposition", "attachment; filename= \"files.zip\""); 流式传输 zip 文件context.HttpContext.Response.AddHeader("content-type", "application/zip");
【解决方案2】:

这是如何在 c# 上下载文件的示例

 string filename = TextBox1.Text;
        Response.ContentType = "application/octet-stream";
        Response.AppendHeader("content-disposition", "attachment;filename=" + filename);
        Response.TransmitFile(Server.MapPath("~/Your file path/" + filename));

        Response.End();

【讨论】:

    猜你喜欢
    • 2012-01-02
    • 2012-01-04
    • 1970-01-01
    • 2015-10-03
    • 1970-01-01
    • 1970-01-01
    • 2018-06-18
    • 2015-05-31
    • 2016-07-09
    相关资源
    最近更新 更多