Popular Posts

Sunday, 25 August 2013

DOWNLOADABLE PATH FROM OTHER DIRECTORY THAN APPLICATION DIRECTORY

CREATE FILE DIRECTORY AT PATH OTHER THAN YOUR APPLICATION PATH


Sometime when you host your application in specific director then you need to keep your downloadable folder that contains material that are needed to be download in other director rather than the hosting environment .
For Example :
you have hosted the application in C DRIVE OF YOUR SERVER.
BUT YOU WANT THE REPORTS DOWNLOAD FOLDER TO BE KEPT IN SOME OTHER DRIVE SAY D: AS IT SAVES THE SPACE OF C DRIVE AND MAKES MORE OF YOUR DATA SAFE AND APPLICATION FAST .

IF YOU GIVE THE LINK OF OTHER DIRECTOR AT DOWNLOAD BUTTON IT WILL GIVE ERROR AS:

The system cannot find the path specified OR


So lets see how to do it.........................................................................................................................


THE CODE DESIGN:create the scenario where ANY file need to be downloaded on button click.


THE CODE BEHIND:

protected void btnDownLoads_Click(object sender, EventArgs e)
    {
   
         string FolderName="WHERE YOUR FILES ARE KEPT"
      string SUBFolderName="SUB FOLDER NAME WHERE YOUR FILES ARE KEPT UNDER        FOLDERNAME";
     
        string filename ="your file need to be downloaded";
   
        string location =FolderName+ "/" + SUBFolderName + "/" + filename;

 

 var filePath = Path.Combine(@"ANOTHER DIRECTORY\PARENT FOLDER\SUBFOLDER\" + FolderName + " \" + SUBFolderName+ "", filename);

//its your choice to keep sub folder ,depending to the hierarchy..
// eg:  var filePath = Path.Combine(@"D:\Project\MainProject FOLDER\" + foldername + " ", filename);
     
Response.ContentType = @"application/pdf";     //here we have selected PDF type file

        Response.AddHeader(
            @"Content-Disposition",
            @"attachment; filename=" + Path.GetFileName(filePath));


        Response.WriteFile(filePath);
        Response.End();

     

............................................................................................................................................................

IT's DONE ...COOL NA.
SO ENJOY



No comments :

Post a Comment