usingSystem;usingSystem.Web;publicclassDownImg:IHttpHandler{//图片下载功能publicvoidProcessRequest(HttpContextcontext){System.Net.WebResponseresponse=null;System.IO.Streamstream=null;stringpath=context.Request.Url.Query.Split(newstring[]{"coverimg="}-C# 下载图片示例-C#语言基础 - IMDN开发者社群-imdn.cn" /> usingSystem;usingSystem.Web;publicclassDownImg:IHttpHandler{//图片下载功能publicvoidProcessRequest(HttpContextcontext){System.Net.WebResponseresponse=null;System.IO.Streamstream=null;stringpath=context.Request.Url.Query.Split(newstring[]{"coverimg="},StringSplitOptions.RemoveEmptyEntries)[1];System.Net.HttpWebRequestrequest=(System.Net.HttpWebRequest)System.Net.WebRequest.Create(path);response=request.GetResponse();stream=response.GetResponseStream();System.IO.MemoryStreammemStream=Txooo.Mobile.HttpTools.CloneStream(stream);byte[]bytes=newbyte[memStream.Length];memStream.Read(bytes,0,bytes.Length);//设置当前流的位置为流的开始memStream.Seek(0,System.IO.SeekOrigin.Begin);context.Response.ContentType="application/octet-stream";//通知浏览器下载文件而不是打开stringfileName=DateTime.Now.Ticks.ToString()".jpg";context.Response.AddHeader("Content-Disposition","attachment;filename="fileName);context.Response.BinaryWrite(bytes);context.Response.Flush();}publicboolIsReusable{get{returnfalse;}}}-IMDN开发者社群-imdn.cn"> usingSystem;usingSystem.Web;publicclassDownImg:IHttpHandler{//图片下载功能publicvoidProcessRequest(HttpContextcontext){System.Net.WebResponseresponse=null;System.IO.Streamstream=null;stringpath=context.Request.Url.Query.Split(newstring[]{"coverimg="},StringSplitOptions.RemoveEmptyEntries)[1];System.Net.HttpWebRequestrequest=(System.Net.HttpWebRequest)System.Net.WebRequest.Create(path);response=request.GetResponse();stream=response.GetResponseStream();System.IO.MemoryStreammemStream=Txooo.Mobile.HttpTools.CloneStream(stream);byte[]bytes=newbyte[memStream.Length];memStream.Read(bytes,0,bytes.Length);//设置当前流的位置为流的开始memStream.Seek(0,System.IO.SeekOrigin.Begin);context.Response.ContentType="application/octet-stream";//通知浏览器下载文件而不是打开stringfileName=DateTime.Now.Ticks.ToString()".jpg";context.Response.AddHeader("Content-Disposition","attachment;filename="fileName);context.Response.BinaryWrite(bytes);context.Response.Flush();}publicboolIsReusable{get{returnfalse;}}} - IMDN开发者社群-imdn.cn">
登录
首页 » C# » C# 下载图片示例

C# 下载图片示例

于 2014-05-29 发布
0 73
下载积分: 1 下载次数: 0

代码说明:

using System;using System.Web;public class DownImg : IHttpHandler { //图片下载功能 public void ProcessRequest(HttpContext context) { System.Net.WebResponse response = null; System.IO.Stream stream = null; string path = context.Request.Url.Query.Split(new string[] { "coverimg=" }, StringSplitOptions.RemoveEmptyEntries)[1]; System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(path); response = request.GetResponse(); stream = response.GetResponseStream(); System.IO.MemoryStream memStream = Txooo.Mobile.HttpTools.CloneStream(stream); byte[] bytes = new byte[memStream.Length]; memStream.Read(bytes, 0, bytes.Length); // 设置当前流的位置为流的开始 memStream.Seek(0, System.IO.SeekOrigin.Begin); context.Response.ContentType = "application/octet-stream"; //通知浏览器下载文件而不是打开 string fileName = DateTime.Now.Ticks.ToString() ".jpg"; context.Response.AddHeader("Content-Disposition", "attachment; filename=" fileName); context.Response.BinaryWrite(bytes); context.Response.Flush(); } public bool IsReusable { get { return false; } }}

下载说明:请别用迅雷下载,失败请重下,重下不扣分!

发表评论

0 个回复

  • 696518资源总数
  • 104269会员总数
  • 31今日下载