Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/29.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
在asp.net中将文件夹从网站下载到客户端计算机_Asp.net_Vb.net_Download_Directory - Fatal编程技术网

在asp.net中将文件夹从网站下载到客户端计算机

在asp.net中将文件夹从网站下载到客户端计算机,asp.net,vb.net,download,directory,Asp.net,Vb.net,Download,Directory,我在服务器上有一个名为“Users”的文件夹 有很多用户在使用这个网站 我为每个用户创建一个不同的文件夹。文件夹的名称将是用户名 我有一些默认的文本文件和一个名为“Upload”的文件夹 任何用户上传的文件都将存储在“上传”文件夹中 因此,对于任何特定用户,他的文件都将位于“Users/Username/upload”中 现在我想从服务器上备份我计算机上的大量数据。所以我想下载名为“用户”的文件夹 我的网站托管在some.com上。 他们没有提供在我的电脑上下载这些数据的便利 所以我决定为自己创

我在服务器上有一个名为“Users”的文件夹

有很多用户在使用这个网站

我为每个用户创建一个不同的文件夹。文件夹的名称将是用户名

我有一些默认的文本文件和一个名为“Upload”的文件夹

任何用户上传的文件都将存储在“上传”文件夹中

因此,对于任何特定用户,他的文件都将位于“Users/Username/upload”中

现在我想从服务器上备份我计算机上的大量数据。所以我想下载名为“用户”的文件夹

我的网站托管在some.com上。 他们没有提供在我的电脑上下载这些数据的便利

所以我决定为自己创建一个下载页面


现在的问题是如何下载这个名为“用户”的文件夹?或者如何将此文件夹转换为zip文件?

首先,我从 解压它并在第三个文件夹中添加对dll的引用

使用部分:

Using System.IO;
Using ICSharp.SharpZipLib.Zip;
代码:

ZipOutputStream zos;
String strBaseDir;

protected void Page_Load(object sender, EventArgs e)
    {
        StartZip(Server.MapPath("directory name"), "filename");
    }


    protected void StartZip(string strPath, string strFileName)
    {
        MemoryStream ms = null;
        Response.ContentType = "application/octet-stream";
        strFileName = HttpUtility.UrlEncode(strFileName).Replace('+', ' ');
        Response.AddHeader("Content-Disposition", "attachment; filename=" + strFileName + ".zip");
        ms = new MemoryStream();
        zos = new ZipOutputStream(ms);
        strBaseDir = strPath + "\\";
        addZipEntry(strBaseDir);
        zos.Finish();
        zos.Close();
        Response.Clear();
        Response.BinaryWrite(ms.ToArray());
        Response.End();
    }


    protected void addZipEntry(string PathStr)
    {
        DirectoryInfo di = new DirectoryInfo(PathStr);
        foreach (DirectoryInfo item in di.GetDirectories())
        {
            addZipEntry(item.FullName);
        }
        foreach (FileInfo item in di.GetFiles())
        {
            FileStream fs = File.OpenRead(item.FullName);
            byte[] buffer = new byte[fs.Length];
            fs.Read(buffer, 0, buffer.Length);
            string strEntryName = item.FullName.Replace(strBaseDir, "");
            ZipEntry entry = new ZipEntry(strEntryName);
            zos.PutNextEntry(entry);
            zos.Write(buffer, 0, buffer.Length);
            fs.Close();
        }
    }
Dim zos as ZipOutputStream
Dim strBaseDir as String

Public Sub btnBackUpDatabase_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnBackUpDatabase.Click

        StartZip(Server.MapPath("~/App_Data"), "Database")

    End Sub

    Protected Sub StartZip(ByVal strPath As String, ByVal strFileName As String)
        Dim ms As IO.MemoryStream = Nothing
        Response.ContentType = "application/octet-stream"
        strFileName = HttpUtility.UrlEncode(strFileName).Replace("+"c, " "c)
        Response.AddHeader("Content-Disposition", "attachment; filename=" & strFileName & ".zip")
        ms = New IO.MemoryStream()
        zos = New ZipOutputStream(ms)
        strBaseDir = strPath & "\"
        addZipEntry(strBaseDir)
        zos.Finish()
        zos.Close()
        Response.Clear()
        Response.BinaryWrite(ms.ToArray())
        Response.[End]()

    End Sub


    Protected Sub addZipEntry(ByVal PathStr As String)
        Dim di As New IO.DirectoryInfo(PathStr)
        For Each item As IO.DirectoryInfo In di.GetDirectories()
            addZipEntry(item.FullName)
        Next
        For Each item As IO.FileInfo In di.GetFiles()
            Dim fs As IO.FileStream = IO.File.OpenRead(item.FullName)
            Dim buffer As Byte() = New Byte(fs.Length - 1) {}
            fs.Read(buffer, 0, buffer.Length)
            Dim strEntryName As String = item.FullName.Replace(strBaseDir, "")
            Dim entry As New ZipEntry(strEntryName)
            zos.PutNextEntry(entry)
            zos.Write(buffer, 0, buffer.Length)
            fs.Close()
        Next
    End Sub
我从你那里得到这个密码

我把它转换成vb.net。下面是VB.NET用户的代码:

进口科:

Imports System.IO
Imports ICSharp.SharpZipLib.Zip
代码:

ZipOutputStream zos;
String strBaseDir;

protected void Page_Load(object sender, EventArgs e)
    {
        StartZip(Server.MapPath("directory name"), "filename");
    }


    protected void StartZip(string strPath, string strFileName)
    {
        MemoryStream ms = null;
        Response.ContentType = "application/octet-stream";
        strFileName = HttpUtility.UrlEncode(strFileName).Replace('+', ' ');
        Response.AddHeader("Content-Disposition", "attachment; filename=" + strFileName + ".zip");
        ms = new MemoryStream();
        zos = new ZipOutputStream(ms);
        strBaseDir = strPath + "\\";
        addZipEntry(strBaseDir);
        zos.Finish();
        zos.Close();
        Response.Clear();
        Response.BinaryWrite(ms.ToArray());
        Response.End();
    }


    protected void addZipEntry(string PathStr)
    {
        DirectoryInfo di = new DirectoryInfo(PathStr);
        foreach (DirectoryInfo item in di.GetDirectories())
        {
            addZipEntry(item.FullName);
        }
        foreach (FileInfo item in di.GetFiles())
        {
            FileStream fs = File.OpenRead(item.FullName);
            byte[] buffer = new byte[fs.Length];
            fs.Read(buffer, 0, buffer.Length);
            string strEntryName = item.FullName.Replace(strBaseDir, "");
            ZipEntry entry = new ZipEntry(strEntryName);
            zos.PutNextEntry(entry);
            zos.Write(buffer, 0, buffer.Length);
            fs.Close();
        }
    }
Dim zos as ZipOutputStream
Dim strBaseDir as String

Public Sub btnBackUpDatabase_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnBackUpDatabase.Click

        StartZip(Server.MapPath("~/App_Data"), "Database")

    End Sub

    Protected Sub StartZip(ByVal strPath As String, ByVal strFileName As String)
        Dim ms As IO.MemoryStream = Nothing
        Response.ContentType = "application/octet-stream"
        strFileName = HttpUtility.UrlEncode(strFileName).Replace("+"c, " "c)
        Response.AddHeader("Content-Disposition", "attachment; filename=" & strFileName & ".zip")
        ms = New IO.MemoryStream()
        zos = New ZipOutputStream(ms)
        strBaseDir = strPath & "\"
        addZipEntry(strBaseDir)
        zos.Finish()
        zos.Close()
        Response.Clear()
        Response.BinaryWrite(ms.ToArray())
        Response.[End]()

    End Sub


    Protected Sub addZipEntry(ByVal PathStr As String)
        Dim di As New IO.DirectoryInfo(PathStr)
        For Each item As IO.DirectoryInfo In di.GetDirectories()
            addZipEntry(item.FullName)
        Next
        For Each item As IO.FileInfo In di.GetFiles()
            Dim fs As IO.FileStream = IO.File.OpenRead(item.FullName)
            Dim buffer As Byte() = New Byte(fs.Length - 1) {}
            fs.Read(buffer, 0, buffer.Length)
            Dim strEntryName As String = item.FullName.Replace(strBaseDir, "")
            Dim entry As New ZipEntry(strEntryName)
            zos.PutNextEntry(entry)
            zos.Write(buffer, 0, buffer.Length)
            fs.Close()
        Next
    End Sub

如果您知道服务器中文件夹的路径,则可能可以使用System.IO.Packaging.ZipPackage或第三方压缩库压缩文件夹。然后,将该文件写入响应流。我在system.io下找不到打包。你能帮我吗?你必须把那个程序集添加到你的引用中。这里有一个例子:我试图添加引用,但在那里我也找不到system.IO.packaging抱歉,我在WindowsBase中找到了它