views:

233

answers:

3

I want to download a file from server to a local host.

i have a code from the net which should work but is not working

     protected void Button4_Click(object sender, EventArgs e)
    {
     //To Get the physical Path of the file(test.txt)
    string filepath = Server.MapPath("test.txt");

    // 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 when the button is clicked the file should be downloaded from the server to the local host computer... but nothing seems to be happening...

i have the test.txt on the desktop of the serer... the save file option also does not come on the client side..

I publish the files and put it in the inetpub folder of the server and run the GUI from the client side.. everything works except this...

any suggestions...please help

this program downloads a file if it is present in the inetpub folder.. instead i want to download from any location within the server...

??

+2  A: 

You mention that test.txt is on the server's desktop. Is it also located right beside the page you're testing? Try either fully-qualifying the path the to the desktop ("C:\Documents and Settings\JohnDoe\Desktop\test.txt") or copying the file to sit alongside the .aspx page.

STW
Also make sure that the IIS account has permission to read wherever said file is stashed at.
Stephen Wrighton
so i am running the GUI from my clients computer will the location address of the file work...also how do i make IIS to read the file.. thanks
`unknown (google)` comment out your file existence check, and you'll see your file full path in your error message
Rubens Farias
+1  A: 

Can you verify that your "if(myFile.Exists)" block is actually executing?

Since you mentioned the file is on the server desktop, it might not be loading properly (check your paths).

Sapph
A: 

ok so i put a Response.Write after

 string filepath = Server.MapPath("test.txt");

and found that file path was pointing to the inetpub folder... so when i put the test.txt in that folder it worked... so the program is right..

but now if the file is any other location in the server how should i modify the program... i am working on it but any suggestions are welcome

Ok i got the answer for any path too

i remove the server.mappath and put the full location instead.. dont know why it was giving problems before.. but now it is working...

Medium trust web applications can't access files outside of the application path. Maybe try elevating your app's permissions: http://msdn.microsoft.com/en-us/library/tkscy493.aspx Of course, this causes new security concerns.
Jim Schubert
but i am logging in and impersonating the user... if he is not impersonated he wont get to download... is there anything else..