Friday, December 17, 2010

How to download any content from server.

for download any content you need to redirect to other page.
sya downloadContent.aspx?FileName="UniqueNameOfContentAsonServer.exe"&Name="NameWhichYouwantToShowInDownloadDialogBox"

call following function on page load event. :)

private void DownlaodContent()
{
if (Request.QueryString["FileName"] == null || Request.QueryString["Name"] == null)
{
return;
}
string _filePath = "";
System.IO.FileInfo _targetFile = null;
try
{
_filePath = Server.MapPath( Request.QueryString["FileName"].ToString());
_targetFile = new System.IO.FileInfo(_filePath);
if (_targetFile.Exists)
{
Response.Clear();
Response.ContentType = "text/plain";
Response.AddHeader("Content-Disposition", "attachment; filename=" + Request.QueryString["Name"].ToString());// + ".txt");
Response.WriteFile(_targetFile.FullName, true);
//_targetFile.Delete(); If you want to delete phycally after download a image
}

}
catch (Exception ex)
{

// Error logging as per existing application.
}
finally
{
_targetFile = null;
Response.Flush();
Response.End();
}
}

Thanks
Mahesh K. Sharma

No comments: