Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为多个文件创建zip,然后将其上载到文件夹中_C#_Asp.net - Fatal编程技术网

C# 为多个文件创建zip,然后将其上载到文件夹中

C# 为多个文件创建zip,然后将其上载到文件夹中,c#,asp.net,C#,Asp.net,我只想将多个文件的zip文件保存到服务器。 首先,它应该创建zip文件,然后将其上载到文件夹 if (FileUpload1.HasFile) { string fileName = Path.GetFileName(FileUpload1.PostedFile.FileName); string fileLocation = Server.MapPath("~/uploads/" + fileName); FileUpload

我只想将多个文件的zip文件保存到服务器。 首先,它应该创建zip文件,然后将其上载到文件夹

    if (FileUpload1.HasFile)
    {

        string fileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
        string fileLocation = Server.MapPath("~/uploads/" + fileName);

        FileUpload1.SaveAs(fileLocation);



        ZipFile createZipFile = new ZipFile();

        createZipFile.AddFile(fileLocation, string.Empty);

        createZipFile.Save(Server.MapPath("~/uploads/CsharpAspNetArticles.zip"));

    }  
如果我理解你的“无问题”问题:)你应该像这样使用IHttpHandler:

using System;
using System.IO;
using System.Web;
using ICSharpCode.SharpZipLib.Zip;

public class ZipHandler : IHttpHandler
{
    public bool IsReusable
    {
        get { return true; }
    }

    public void ProcessRequest(HttpContext ctx)
    {
        var path = HttpUtility.UrlDecode(ctx.Request.QueryString["folder"]);
        if (path == null)
        {
            return;
        }
        var folderName = Path.GetFileName(path);
        if (folderName == String.Empty)
        {
            folderName = "root";
        }

        int folderOffset = ctx.Server.MapPath("~").Length;

        using (var zipStream = new ZipOutputStream(ctx.Response.OutputStream))
        {
            ctx.Response.Clear();
            ctx.Response.BufferOutput = false;
            ctx.Response.AddHeader("Content-Disposition", "attachment; filename=" + folderName + ".zip");
            ctx.Response.AddHeader("Content-Type", "application/zip");

            zipStream.SetLevel(3);

            CompressFolder(path, zipStream, folderOffset);

            ctx.Response.Flush();
        }
    }

    private static void CompressFolder(string path, ZipOutputStream zipStream, int folderOffset)
    {
        string[] files = Directory.GetFiles(path);
        foreach (string filename in files)
        {
            try
            {
                using (var streamReader = File.OpenRead(filename))
                {
                    var fi = new FileInfo(filename);

                    string entryName = filename.Substring(folderOffset);
                    entryName = ZipEntry.CleanName(entryName);

                    var newEntry = new ZipEntry(entryName)
                    {
                        DateTime = fi.LastWriteTime,
                        Size = fi.Length
                    };

                    zipStream.PutNextEntry(newEntry);

                    streamReader.CopyTo(zipStream, 4096);

                    zipStream.CloseEntry();
                }
            }
            catch (IOException) { }
        }

        var folders = Directory.GetDirectories(path);
        foreach (string folder in folders)
        {
            CompressFolder(folder, zipStream, folderOffset);
        }
    }
}

你的问题是?嗨…我的问题是如果有多个文件,那么首先创建zip,然后在上载按钮上将该zip文件上载到文件夹。单击这仍然不是问题,尝试使用诸如“如何”、“为什么”、“什么”或“谁”之类的词,并在句子结尾处使用?。如何通过将多个文件添加到zip并将其保存到文件夹中来上载这些文件??