Click here to Skip to main content
15,896,606 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to open different documents using its own application in a browser.
Posted

1 solution

Try like this

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.");
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900