Browse Files Under a directory Using GridView in a web application

Suppose, in a web application we need to create some files (say xml reports) and then show it to the user. One of the simpler ways to view those files would be through a popup() page. This is what is explained below:

Solution: Let our web application display XML files from a specific directory location (say “C:\XML Files”) 

Now we need to use DirectoryInfo object, FileInfo object, GridView grdFileList.

In short, DirectoryInfo Objects are used to expose properties related to directories like,directory name, directory creation date, list of files & directories within the specified directory, full path of directory etc.

Similarly FileInfo Objects expose properties related to files lke, file name, file creation time, file full path name etc.

Now, the Function can be defined more or less like this,

private void ShowDirectoryContents(string path)
{
lblDisplay.Text= "List of XML Files Under Directory are : " + path;
 
 
//Define the current directory (Here, it is C:\XML Files)
DirectoryInfo dir = new DirectoryInfo(path);
 
 
//Get the FileInfo Objects
FileInfo[] files = dir.GetFiles();
 
 
 
//Bind FileInfo objects to Gridview
grdFileList.DataSource = files;
 
 
Page.DataBind();
 
//Clear any selection
grdFileList.SelectedIndex = -1; 
}

Call the above defined function  in the page_load() 

ShowDirectoryContents(@”C:\XML Files”);

As grdFileList is bound to FileInfo[] Objects, it can use FileInfo Properties like (Name,FullName,CreationTime, etc) to display as individual columns.

So in aspx page (Html view), GridView Code should be more or less looks like this,

Now to view our selected files, use SelectedIndesChanged Event of the GridView..

protected void grdFileList_SelectedIndexChanged(object sender, EventArgs e)

{

/*******************************************************

Note: FullName(FileInfo.Fullname) of the file is specified as Data Key for GridView

*******************************************************/

string url = @”ShowXMLFile.aspx?FullPath=” + grdFileList.SelectedDataKey.Value;

ClientScript.RegisterStartupScript(this.GetType(), “newWindow”, String.Format(“window.open(‘{0}’);”, url.Replace(“\\”, “\\\\”)));

}

We will use FullPath of the selected file as the QueryString to another WebPage Called (ShowXMLFile.aspx).

This web page will be used to display xml files.

in the Page_Load() of ShowXMLFile.aspx, write

protected void Page_Load(object sender, EventArgs e)

{

   string FullPath = Request.QueryString[“FullPath”];

   FileInfo file = new FileInfo(FullPath);

if (file.Exists)

{

    Response.ClearContent();

    Response.AddHeader(“Content-Disposition”, “attachment; filename=” + file.Name);

    Response.AddHeader(“Content-Length”, file.Length.ToString());

    Response.ContentType = “application/xml”;

    Response.TransmitFile(file.FullName);

    Response.End(); 

}

}

(NOTE: ShowXMLFile.aspx is specially designed to view XML files only. )

150 150 Burnignorance | Where Minds Meet And Sparks Fly!