下载指定路径下的图片案例,源代码

#region 下载珠宝图片
protected void lbtnDown_Click(object sender, EventArgs e)
{
int pid = ConvertHelper.GetInteger(Request["PID"]);
pds = p.GetModel(pid);
// 保存文件的虚拟路径
string Url = "../uploadpic\" + pds.Picture;//<img src="../uploadpic/20150213/wm201521317692.jpg" />
// 保存文件的物理路径
string FullPath = HttpContext.Current.Server.MapPath(Url);
// 初始化FileInfo类的实例,作为文件路径的包装
FileInfo FI = new FileInfo(FullPath);
// 判断文件是否存在
if (FI.Exists)
{
// 将文件保存到本机
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment;filename=" + Server.UrlEncode(FI.Name));
Response.AddHeader("Content-Length", FI.Length.ToString());
Response.ContentType = "application/octet-stream";
Response.Filter.Close();
Response.WriteFile(FI.FullName);
Response.End();
}
}

#endregion

原文地址:https://www.cnblogs.com/azzhang/p/4291854.html