views:

144

answers:

2

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

+1  A: 

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

Rubens Farias
but in this i have to go in the folder and select the files i want to zip... instead what if the user just want to zip a folder which has 100 files in it...
No; though code you can to select all files to be added on zip, without user interaction
Rubens Farias
I got what you are trying to say but what if the user wants to select 3 folders from the C: drive or there are other folders in one of the folders selected... i tried putting a folder in the folder which has files and it does not read it...
but i must say you were very helpful...
ok i have just been told i cannot zip it before..
sorry... but is there no other way to do this are u sure???
No, two separate downloads on single HTTP request is technically impossible, sorry;
Rubens Farias
+1  A: 

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

dcp
but in this i have to go in the folder and select the files i want to zip... instead what if the user just want to zip a folder which has 100 files in it...