using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
public class Utility:System.Web.UI.Page
{
string filepath = string.Empty;
public Utility()
{
//
// TODO: Add constructor logic here
//
}
public void DownloadFile(string FileName)
{
///get the file
///
try
{
if (HttpContext.Current.Request.RawUrl.Contains("Admin"))
{
filepath = Server.MapPath("~/UploadFiles/" + FileName);
}
else
{
filepath = Server.MapPath("UploadFiles/" + FileName);
}
string[] extension = FileName.Split('.');
///get the file stream to get the file length
System.IO.FileStream fs = new System.IO.FileStream(filepath, System.IO.FileMode.Open);
///set the content type
HttpContext.Current.Response.ContentType = "application/" + extension[1];
///set Content-Disposition
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment; filename=" + FileName);
///get the file size
long filesize = fs.Length;
fs.Close();
///set the content length to the size of the file
///this will chop off the extra junk that may be sent by the ASP.NET runtime along with your file
HttpContext.Current.Response.AddHeader("Content-Length", filesize.ToString());
///write the file to the browser
HttpContext.Current.Response.WriteFile(filepath);
///flush it
HttpContext.Current.Response.Flush();
}
catch (Exception ex)
{
}
}
}
Thank you very much!!!
ReplyDeleteIt's working great.....