从服务器下载文件夹到本地主机

时间:2010-01-11 16:42:55

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

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

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

  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 ......

感谢...

我需要在没有拉链的情况下这样做...请建议一种方式...

2 个答案:

答案 0 :(得分:1)

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

Downloading Multiple Files as a Zip File Using GridView and SharpZipLib

答案 1 :(得分:1)

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