#region File download
private void forceDownload(string filePath, bool forceDownload)
{
try
{
string path = MapPath(filePath);
string name = Path.GetFileName(filePath);
string extension = Path.GetExtension(filePath);
string contentType = string.Empty;
switch (extension.ToLower())
{
case ".jpg":
contentType = "image/JPEG";
break;
case ".gif":
contentType = "image/GIF";
break;
case ".html":
case ".htm":
contentType = "image/HTML";
break;
case ".pdf":
contentType = "Application/pdf";
break;
case ".doc":
case ".rtf":
//for Microsoft Word files
contentType = "Application/msword";
break;
case ".xls":
//for Microsoft Excel files
contentType = "Application/x-msexcel";
break;
case ".txt":
contentType = "text/plain";
break;
}
if (forceDownload)
{
Response.AppendHeader("content-disposition", "attachment; filename=" + name);
}
if (contentType != string.Empty)
//Set the appropriate ContentType.
Response.ContentType = contentType;
//Write the file directly to the HTTP content output stream.
Response.WriteFile(path);
Response.End();
}
catch (Exception errmsg)
{
Response.Write(errmsg.Message.ToString());
}
}
#endregion
protected void LinkButtonDownLoad_Click(object sender, EventArgs e)
{
string filePath = ".\\uploadData\\" + LinkButtonDownLoad.Text.Trim();
if (File.Exists(Server.MapPath(filePath)))
{
forceDownload(filePath, true);
}
else
{
ClientScript.RegisterStartupScript(this.GetType(), "onload", "alert('Sorry, file not found!');", true);
}
}
}
private void forceDownload(string filePath, bool forceDownload)
{
try
{
string path = MapPath(filePath);
string name = Path.GetFileName(filePath);
string extension = Path.GetExtension(filePath);
string contentType = string.Empty;
switch (extension.ToLower())
{
case ".jpg":
contentType = "image/JPEG";
break;
case ".gif":
contentType = "image/GIF";
break;
case ".html":
case ".htm":
contentType = "image/HTML";
break;
case ".pdf":
contentType = "Application/pdf";
break;
case ".doc":
case ".rtf":
//for Microsoft Word files
contentType = "Application/msword";
break;
case ".xls":
//for Microsoft Excel files
contentType = "Application/x-msexcel";
break;
case ".txt":
contentType = "text/plain";
break;
}
if (forceDownload)
{
Response.AppendHeader("content-disposition", "attachment; filename=" + name);
}
if (contentType != string.Empty)
//Set the appropriate ContentType.
Response.ContentType = contentType;
//Write the file directly to the HTTP content output stream.
Response.WriteFile(path);
Response.End();
}
catch (Exception errmsg)
{
Response.Write(errmsg.Message.ToString());
}
}
#endregion
protected void LinkButtonDownLoad_Click(object sender, EventArgs e)
{
string filePath = ".\\uploadData\\" + LinkButtonDownLoad.Text.Trim();
if (File.Exists(Server.MapPath(filePath)))
{
forceDownload(filePath, true);
}
else
{
ClientScript.RegisterStartupScript(this.GetType(), "onload", "alert('Sorry, file not found!');", true);
}
}
}