【问题标题】:download a folder from server to local host从服务器下载文件夹到本地主机
【发布时间】:2011-01-03 20:03:03
【问题描述】:

我有一个代码来下载存储在服务器上特定位置的特定文件...

例如,我在服务器的 C: 驱动器中有一个 untitled.bmp 文件,我使用特定代码将其下载到本地主机...

  protected void Button1_Click(object sender, EventArgs e)
    {
         string filepath = (@"C:\untitled.bmp");



        // Create New instance of FileInfo class to get the properties of the file being downloaded
        FileInfo myfile = new FileInfo(filepath);

        // Checking if file exists
        if (myfile.Exists)
        {
            // Clear the content of the response
            Response.ClearContent();

            // Add the file name and attachment, which will force the open/cancel/save dialog box to show, to the header
            Response.AddHeader("Content-Disposition", "attachment; filename=" + myfile.Name);

            // Add the file size into the response header
            Response.AddHeader("Content-Length", myfile.Length.ToString());

            // Set the ContentType
            Response.ContentType = ReturnExtension(myfile.Extension.ToLower());

            // Write the file into the response (TransmitFile is for ASP.NET 2.0. In ASP.NET 1.1 you have to use WriteFile instead)
            Response.TransmitFile(myfile.FullName);

            // End the response
            Response.End();
        }
    }


    private string ReturnExtension(string fileExtension)
    {
        switch (fileExtension)
        {
            case ".htm":
            case ".html":
            case ".log":
                return "text/HTML";
            case ".txt":
                return "text/plain";
            case ".doc":
                return "application/ms-word";
            case ".tiff":
            case ".tif":
                return "image/tiff";
            case ".asf":
                return "video/x-ms-asf";
            case ".avi":
                return "video/avi";
            case ".zip":
                return "application/zip";
            case ".xls":
            case ".csv":
                return "application/vnd.ms-excel";
            case ".gif":
                return "image/gif";
            case ".jpg":
            case "jpeg":
                return "image/jpeg";
            case ".bmp":
                return "image/bmp";
            case ".wav":
                return "audio/wav";
            case ".mp3":
                return "audio/mpeg3";
            case ".mpg":
            case "mpeg":
                return "video/mpeg";
            case ".rtf":
                return "application/rtf";
            case ".asp":
                return "text/asp";
            case ".pdf":
                return "application/pdf";
            case ".fdf":
                return "application/vnd.fdf";
            case ".ppt":
                return "application/mspowerpoint";
            case ".dwg":
                return "image/vnd.dwg";
            case ".msg":
                return "application/msoutlook";
            case ".xml":
            case ".sdxl":
                return "application/xml";
            case ".xdp":
                return "application/vnd.adobe.xdp+xml";
            default:
                return "application/octet-stream";
        }
    }

现在我的问题是如何下载一个包含多个文件的文件夹..有没有办法???

例如,我在 C: 驱动器中有一个名为 recovery 的文件夹,其中包含两个文件 untitled.bmp 和 test.txt...

谢谢...

我需要在不压缩的情况下执行此操作...请提出一种方法...

【问题讨论】:

    标签: c# asp.net download localhost server-side


    【解决方案1】:

    您可以将文件压缩在一起(例如 untitled.bmp 和 test.txt),然后将它们下载为单个文件。

    【讨论】:

    • 但是在这个我必须进入文件夹并选择我想要压缩的文件......而不是如果用户只想压缩一个包含 100 个文件的文件夹......
    【解决方案2】:

    对于多个文件,您需要将它们打包在一起,例如,一个 ZIP 文件。

    Downloading Multiple Files as a Zip File Using GridView and SharpZipLib

    【讨论】:

    • 但是在这个我必须进入文件夹并选择我想要压缩的文件......而不是如果用户只想压缩一个包含 100 个文件的文件夹......
    • 否;虽然代码您可以选择要添加到 zip 中的所有文件,而无需用户交互
    • 我明白了你的意思,但是如果用户想从 C: 驱动器中选择 3 个文件夹,或者其中一个文件夹中有其他文件夹...我试着放一个文件夹在有文件的文件夹中,它不读取它...
    • 不,单个 HTTP 请求上的两个单独下载在技术上是不可能的,抱歉;
    猜你喜欢
    • 2022-11-24
    • 1970-01-01
    • 1970-01-01
    • 2019-02-16
    • 1970-01-01
    • 1970-01-01
    • 2011-10-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多