Getting file from the server
The following code shows one way to retrieve the file from the server. In this case, the file is displayed in the browser.
private void butView_Click(object sender, System.EventArgs e)
{
// Get selected file name
string strFilename = lstServerFiles.SelectedItem.ToString();
// Display as web page
Response.WriteFile(Request.MapPath("./uploadedfiles" + "\\" + strFilename));
}
FOR DOWNLOADING FILES TO THE CLIENT COMPUTER
using System.IO; // May not need to use if you use fully qualified name
private void butView_Click(object sender, System.EventArgs e)
{
// Get selected file name
string strFilename = lstServerFiles.SelectedItem.ToString();
DownloadFile(Request.MapPath(".\\uploadedfiles" + "\\" + strFilename),true);
}
private void DownloadFile( string fname, bool forceDownload )
{
string path = System.IO.Path.GetFileName(fname);
string name = System.IO.Path.GetFileName( path );
string ext = System.IO.Path.GetExtension( path );
string type = "";
// set known types based on file extension
if ( ext != null )
{
switch( ext.ToLower() )
{
case ".htm":
case ".html":
type = "text/HTML";
break;
case ".txt":
type = "text/plain";
break;
case ".doc":
case ".rtf":
type = "Application/msword";
break;
}
}
if ( forceDownload )
{
Response.AppendHeader( "content-disposition", "attachment; filename=" + name );
}
if ( type != "" )
Response.ContentType = type;
Response.WriteFile( fname );
Response.End();
}