图片下载到本机 如题 、谢谢! 最好是下载图片保存到选则的位置 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 string filepath = Server.MapPath("~/xx.jpg");string filename = System.IO.Path.GetFileName(filepath);Response.Clear();Response.ContentType = "application/octet-stream";Response.AddHeader("Content-Disposition", "attachment; filename=" + filename);Response.Flush();Response.WriteFile(filepath); <a value='<%#Eval("FilePath") %>' href='../Ajax/AjaxDownFile.ashx?id=<%#Eval("FileId")%>'> 【下载】</a>using System;using System.Collections;using System.Data;using System.Linq;using System.Web;using System.Web.Services;using System.Web.Services.Protocols;using System.Xml.Linq;namespace Asiastar.NR.Ajax{ /// <summary> /// $codebehindclassname$ 的摘要说明 /// </summary> [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] public class Handler1 : IHttpHandler { public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; string id = context.Request["id"].ToString();//获取资源的编号 System.IO.Stream iStream = null; byte[] buffer = new Byte[10000]; int length; long dataToRead; NRBLL.File bf = new Asiastar.NRBLL.File(); Guid guid = new Guid(id); if (bf.FN_SerchPathByFileId(guid).Tables[0].Rows[0]["FilePath"] != null)//判断数据库路径是否存在 { string filepath = bf.FN_SerchPathByFileId(guid).Tables[0].Rows[0]["FilePath"].ToString();//获取资源完整路径 D:\资源文件\600cc139-14cf-448e-9e50-daa972d35e01.jpg string Oidfilename = bf.FN_SerchPathByFileId(guid).Tables[0].Rows[0]["FileNam"].ToString();//旧文件名称 //string filename = System.IO.Path.GetFileName(filepath);//获取文件名称+后缀名 600cc139-14cf-448e-9e50-daa972d35e01.JPG //int index = filepath.IndexOf("."); //string filetype = filepath.Substring(index).ToLower();//后缀名 //string newfilename = Oidfilename; //string filepath1 = bf.FN_SerchPathByFileId(guid).Tables[0].Rows[0]["FilePath"].ToString().Substring(0,filepath.Length - 8); try { string fileName = HttpUtility.UrlEncode(System.Text.Encoding.UTF8.GetBytes(Oidfilename));//解码(注意这里2层解码) Oidfilename = Oidfilename.Replace("+", "%20"); //将“+”替换成“空格” iStream = new System.IO.FileStream(filepath, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read); dataToRead = iStream.Length; context.Response.ContentType = "application/octet-stream"; context.Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(Oidfilename, System.Text.Encoding.UTF8)); //下载的时候下载原来的文件名称 while (dataToRead > 0) { if (context.Response.IsClientConnected) { length = iStream.Read(buffer, 0, 10000); context.Response.OutputStream.Write(buffer, 0, length); context.Response.Flush(); buffer = new Byte[10000]; dataToRead = dataToRead - length; } else { dataToRead = -1; } } } catch (Exception ex) { NR.Error.Log.LogType(ex.ToString()); } finally { if (iStream != null) { iStream.Close(); } } } else { NR.Error.Log.LogType("找不到文件!"); } } public bool IsReusable { get { return false; } } }}filepath指定你的图片路径就可以实现下载了 下载的时候有直接打开和选择保存路径 这个得借助于ActiveX控件来实现。普通的方式是直接弹出一个下载窗口,然后提示用户选择保存位置。默认界面下载中下载完毕 网上有一个HTTP文件下载控件。 http://www.cnblogs.com/xproer/archive/2011/03/26/1996322.html 晕,用ActiveX下载,还不如直接IE另存为,然后去存放目录里面找 可以用用ActiveX下载 代码 上面有人写了 用用看那 其实这也是为了满足不同的需求。每个企业,每个系统的需求都不一样。一般的用户下载单个小文件可能是IE另存为,下载大文件用迅雷。如果文件多的话用IE另存为这个就非常麻烦了。但是如果同时要下载5个小文件,开迅雷的话又太慢了。 希望大家给推荐本可以提高asp.net的书名 奇怪!用DropDownList1做友情链接需点第一次后,再点第二次才执行事件。 JS导出页面DIV内容到execl 请教一个DropDownList的绑定问题 CSS的revealTrans滤镜,不能随机切换样式,请求支援 literal控件用途和用法? 两个数据库小问题,谢谢 asp:chart怎么控制x轴的刻度间隔 问一个简单问题,并希望高手深入一下。谢谢! 请问sql server中将工a表的数据插入b表中,很多数据时如何判断已完成。 MVC3 中control 能获取到view中定义的变量吗? 求教
string filename = System.IO.Path.GetFileName(filepath);
Response.Clear();
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment; filename=" + filename);
Response.Flush();
Response.WriteFile(filepath);
<a value='<%#Eval("FilePath") %>' href='../Ajax/AjaxDownFile.ashx?id=<%#Eval("FileId")%>'>
【下载】</a>using System;
using System.Collections;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;namespace Asiastar.NR.Ajax
{
/// <summary>
/// $codebehindclassname$ 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Handler1 : IHttpHandler
{ public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain"; string id = context.Request["id"].ToString();//获取资源的编号
System.IO.Stream iStream = null;
byte[] buffer = new Byte[10000];
int length;
long dataToRead;
NRBLL.File bf = new Asiastar.NRBLL.File();
Guid guid = new Guid(id);
if (bf.FN_SerchPathByFileId(guid).Tables[0].Rows[0]["FilePath"] != null)//判断数据库路径是否存在
{
string filepath = bf.FN_SerchPathByFileId(guid).Tables[0].Rows[0]["FilePath"].ToString();//获取资源完整路径 D:\资源文件\600cc139-14cf-448e-9e50-daa972d35e01.jpg
string Oidfilename = bf.FN_SerchPathByFileId(guid).Tables[0].Rows[0]["FileNam"].ToString();//旧文件名称
//string filename = System.IO.Path.GetFileName(filepath);//获取文件名称+后缀名 600cc139-14cf-448e-9e50-daa972d35e01.JPG
//int index = filepath.IndexOf(".");
//string filetype = filepath.Substring(index).ToLower();//后缀名
//string newfilename = Oidfilename;
//string filepath1 = bf.FN_SerchPathByFileId(guid).Tables[0].Rows[0]["FilePath"].ToString().Substring(0,filepath.Length - 8);
try
{
string fileName = HttpUtility.UrlEncode(System.Text.Encoding.UTF8.GetBytes(Oidfilename));//解码(注意这里2层解码)
Oidfilename = Oidfilename.Replace("+", "%20"); //将“+”替换成“空格”
iStream = new System.IO.FileStream(filepath, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read);
dataToRead = iStream.Length;
context.Response.ContentType = "application/octet-stream";
context.Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(Oidfilename, System.Text.Encoding.UTF8)); //下载的时候下载原来的文件名称
while (dataToRead > 0)
{
if (context.Response.IsClientConnected)
{
length = iStream.Read(buffer, 0, 10000);
context.Response.OutputStream.Write(buffer, 0, length);
context.Response.Flush();
buffer = new Byte[10000];
dataToRead = dataToRead - length;
}
else
{
dataToRead = -1;
}
} }
catch (Exception ex)
{
NR.Error.Log.LogType(ex.ToString());
}
finally
{
if (iStream != null)
{
iStream.Close();
}
}
}
else
{
NR.Error.Log.LogType("找不到文件!");
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
}filepath指定你的图片路径就可以实现下载了 下载的时候有直接打开和选择保存路径
如果文件多的话用IE另存为这个就非常麻烦了。但是如果同时要下载5个小文件,开迅雷的话又太慢了。