idrisgani
March 31st, 2009, 08:47 AM
hi. i just wanna open a file which is created from the server using webbrowser. i tried with the following code. but its downloading the file from the server to local. i just want to open the file in a new popup window. please help me. thanks in advance
void Page_Load ( object sender, EventArgs e )
string strRequest = Request.QueryString["file"]; //-- if something was passed to the file querystring
if ( strRequest != "" )
{
//get absolute path of the file
// the file is passed like a web address /images/myimage.jpg
string path = Server.MapPath(strRequest); //get file object as FileInfo
System.IO.FileInfo file = new System.IO.FileInfo(path); //-- if the file exists on the server
if ( file.Exists ) //set appropriate headers
{
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
Response.AddHeader("Content-Length", file.Length.ToString());
Response.ContentType = "application/octet-stream";
// write file to browser
Response.WriteFile(file.FullName);
Response.End();
}
else
{
// if file does not exist
Response.Write("This file does not exist.");
}
}
else
{
//nothing in the URL as HTTP GET
Response.Write("Please provide a file to download.");
}
}
void Page_Load ( object sender, EventArgs e )
string strRequest = Request.QueryString["file"]; //-- if something was passed to the file querystring
if ( strRequest != "" )
{
//get absolute path of the file
// the file is passed like a web address /images/myimage.jpg
string path = Server.MapPath(strRequest); //get file object as FileInfo
System.IO.FileInfo file = new System.IO.FileInfo(path); //-- if the file exists on the server
if ( file.Exists ) //set appropriate headers
{
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
Response.AddHeader("Content-Length", file.Length.ToString());
Response.ContentType = "application/octet-stream";
// write file to browser
Response.WriteFile(file.FullName);
Response.End();
}
else
{
// if file does not exist
Response.Write("This file does not exist.");
}
}
else
{
//nothing in the URL as HTTP GET
Response.Write("Please provide a file to download.");
}
}