开发者

download a folder from server to local host

开发者 https://www.devze.com 2022-12-16 14:18 出处:网络
I have a code to downolad a particular file that is stored at a particular location on the server... for example i have a file untitled.bmp in the C: drive of my server and i download this to my loca

I have a code to downolad a particular file that is stored at a particular location on the server...

for example i have a file untitled.bmp in the C: drive of my server and i download this to my localhost using the particular code...

  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";
        }
    }

now my problem is how do i download a folder with multiple files in it.. is there a way???

for example i have a folder named recovery in C: drive which contains two files untitled.bmp and test.txt...

thanks...

I need to do this without zipping... please suggest a way...


For multiple files, you'll need to pack them together as, for instance, a ZIP file.

Downloading Multiple Files as a Zip File Using GridView and SharpZipLib


You could zip the files together (e.g. untitled.bmp and test.txt) and then download them as a single file.

0

精彩评论

暂无评论...
验证码 换一张
取 消