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 154
下载积分: 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 个回复

  • 计算你人生 剩余几个月 实例源码
    计算你人生 剩余几个月 实例源码
    2014-06-11下载
    积分:1
  • PHONE 手机端 蛋糕房系统源码下载
    API
    2014-07-25下载
    积分:1
  • RS-232接口 示例源码下载 SerialPortTerminal
    RS-232接口 示例源码下载 SerialPortTerminal
    2021-04-20 23:48:49下载
    积分:1
  • .NET Reflector 5.0 使用例子(反编译软件)
    .NET Reflector 5.0 Release Notes 28 December 2006 Reflector is a class browser for .NET components. It allows browsing and searching the meta data, IL instructions, resources and XML documentation stored in a .NET assembly. Reflector was first released in October 2000 running on .NET Framework 1.0 Beta. LINQ and .NET Framework 3.5: Reflector supports query expressions and other concepts introduced in C# 3.5. To enable this feature select ".NET 3.5" under View, Options, Disassembler, Optimization. .NET Framework compatibility: Reflector runs on all versions of the .NET Framework using its own assembly loading infrastructure which does not rely on the Reflection API. For example, Reflector can load .NET Framework 2.0 assemblies without having the .NET Framework 2.0 installed. Assembly Lists: When launched for the first time, Reflector allows you to choose a default set of assemblies. The list of assemblies is then stored in the Reflector.cfg file and will be loaded next time you open the program. Reflector allows creating multiple assembly lists and switching between the lists using the File, Open List dialog. To choose a different set of default assemblies for the current assembly list you should remove all assemblies from the list (DEL) and invoke the Refresh command (F5). Assembly Cache: When resolving an assembly reference, Reflector will first search the local path next to the assembly holding the reference and then falls back to the cache directories defined in the Reflector.cfg file. Reflector does not search the Global Assembly Cache (GAC) unless you add "%SystemRoot%Assembly" to the cache directories list. Assembly Versioning: By default, assembly version numbers are ignored when resolving type and member references. You can enable side-by-side versioning in the options dialog but it is suggested to avoid this if possible. Add-Ins: Information about the Reflector add-in model can be found here. The most recent version of .NET Reflector is available here. Disclaimer: This software is provided "AS IS" with no warranties of any kind. The entire risk arising out of the use or performance of the software is with you.
    2015-06-15下载
    积分:1
  • C#自定义控件库-漂亮的按钮
    列举了一些漂亮的按钮和文本输入框,包括demo
    2017-11-02下载
    积分:1
  • A4打印(winform源码)
    【实例简介】
    2021-08-06 00:31:02下载
    积分:1
  • C#操作ini文件实例
    C#操作ini文件
    2013-01-20下载
    积分:1
  • c#微信公众平台源代码
    c#微信公众平台源代码
    2013-12-30下载
    积分:1
  • wpf 漂亮的UI界面(含左侧导航菜单)
    wpf 漂亮的UI界面(含左侧导航菜单)
    2019-03-19下载
    积分:1
  • C参考手册
    1.C参考手册.chm 2.C函数查询.chm 3.C语言标准库函数大全.chm 4.C语言库函数使用大全CHM版.chm 5.C语言库函数速查手册.chm
    2021-05-06下载
    积分:1
  • 696516资源总数
  • 106914会员总数
  • 0今日下载