html A标签 href 如何实现下载 图片。
html A标签 href 如何实现下载 图片。
<html>html 图片 标签
<head>
<meta http-equiv="Content-Disposition" content="attachment;">
</head>
<body>
<a target="_blank" href="http://stimgcn1.s-msn.com/msnportal/hp/2012/10/18/b24b7623-cddc-408d-a01a-c6c3582a612b.png" >测试图片下载!</a>
</body>
</html>
/// <summary>
/// 在 ASP.NET 事件处理程序(例如,某页或某个 XML Web service)执行完毕时发生(4.5、4、3.5、3.0、2.0、1.1、1.0)。
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void PostRequestHandlerExecuteEvent(object sender, EventArgs e)
{
HttpApplication ha = (HttpApplication)sender;
string filePath = ha.Request.FilePath;
if (filePath.Contains(".") && filePath.Remove(0, filePath.LastIndexOf('.')) == ".png")
{
using (FileStream fs = new FileStream(ha.Server.MapPath(filePath), FileMode.Open, FileAccess.Read))
{
int fslength = (int)fs.Length;
byte[] b = new byte[fslength];
fs.Read(b, 0, fslength);
ha.Response.ContentType = "application/octet-stream";
ha.Response.Write(b);
ha.Response.Flush();
ha.Response.End();
}
}
}
<html>
<head>
</head>
<body>
<img src="http://stimgcn1.s-msn.com/msnportal/hp/2012/10/18/b24b7623-cddc-408d-a01a-c6c3582a612b.png" />
<a target="_blank" href="http://stimgcn1.s-msn.com/msnportal/hp/2012/10/18/b24b7623-cddc-408d-a01a-c6c3582a612b.png" >测试图片下载!</a>
</body>
</html>
/// <summary>
/// 文件下载
/// </summary>
/// <param name="s_fileName"></param>
public static void downloadfile(string s_fileName)
{
HttpContext.Current.Response.ContentType = "application/ms-download";
string s_path = HttpContext.Current.Server.MapPath("~/") + s_fileName;
System.IO.FileInfo file = new System.IO.FileInfo(s_path);
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader("Content-Type", "application/octet-stream");
HttpContext.Current.Response.Charset = "utf-8";
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(file.Name, System.Text.Encoding.UTF8));
HttpContext.Current.Response.AddHeader("Content-Length", file.Length.ToString());
HttpContext.Current.Response.WriteFile(file.FullName);
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.End();
}