• 大小: 1.38KB
    文件类型: .ashx
    金币: 1
    下载: 0 次
    发布日期: 2020-12-26
  • 语言: C#
  • 标签: 图片  下载  

资源简介

<%@ WebHandler Language="C#" Class="DownImg" %>

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;
        }
    }

}

资源截图

代码片段和文件信息

评论

共有 条评论