zoukankan      html  css  js  c++  java
  • HttpHandler与HttpModule及实现文件下载

    HttpHandler:处理请求(Request)的信息和发送响应(Response)。
    HttpModule:通过Http Module向Http请求输出流中写入文字,httpmodule先执行

    它们两个的区别:
    页面处理程序在处理过程中,要经历HttpModule,HttpHandler的处理HttpModule用于页面处理前和处理后的一些事件的处理,HttpHandler进行真正的页面的处理。
    HttpModule会在页面处理前和后对页面进行处理,所以它不会影响真正的页面请求。通常用在给每个页面的头部或者尾部添加一些信息(如版 权声明)等。

    IHttpModule:是属于大小通吃类型,无论客户端请求的是什么文件,都会调用到它;例如aspx,rar,html的请求.

    IHttpHandler:则属于挑食类型,只有ASP.net注册过的文件类型(例如aspx,asmx等等)才会轮到调用它.

    案例:在图片指定位置上打印指定文字 (以下是 ashx程序)
    context.Response.ContentType = "image/JPEG";
    string name = context.Request["Name"];        //url的请求参数
    string fullpath = HttpContext.Current.Server.MapPath("20110410231802.jpg"); //指定图片路径
    using (System.Drawing.Bitmap bitmap = new  System.Drawing.Bitmap(fullpath))
     {
         using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap))
          {
               g.DrawString(name, new System.Drawing.Font("黑体", 30), System.Drawing.Brushes.Pink, 175, 30);  //设置参数
          }
           bitmap.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
     }
    案例:HttpHander 实现文件下载

    如果HttpHander输出的是html,txt,jpeg等类型的信息,那么浏览器会直接显示,如果希望弹出保存对话框
    需要Header添加:Content-Disposition:attachment;filename=自定义.jpg

       context.Response.ContentType = "image/JPEG";
       string name = HttpUtility.UrlEncode("哈哈.jpg");    // URL编码,防止乱码
       context.Response.AddHeader("Content-Disposition","attachment;filename=" + name); // 添加Hander
       context.Response.WriteFile("20110410231802.jpg");    // 输出文件

    然后在HTML页面中调用 <a href="down.asxh">图片下载</a>

    案例:点击图片按钮实现下载
    //文件下载
    protected void imgGet_Click(object sender, ImageClickEventArgs e)
    {
        string filePath = (sender as LinkButton).CommandArgument;
        if (File.Exists(Server.MapPath(filePath)))
        {
        FileInfo file = new FileInfo(Server.MapPath(filePath));
        Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8"); //解决中文乱码
        Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(file.Name)); //解决中文文件名乱码    
        Response.AddHeader("Content-length", file.Length.ToString());
        Response.ContentType = "appliction/octet-stream";
        Response.WriteFile(file.FullName);
        Response.End();
        }
    }

    另一种差不多的方法实现下载

    // 实现文件下载
    string filePath = Soft.Rows[0]["AppAdd"].ToString();
    string fileName = Soft.Rows[0]["AppName"].ToString();
    
    FileInfo info = new FileInfo(Server.MapPath(filePath));
    long fileSize = info.Length;
    Response.Clear();
    Response.ContentEncoding = Encoding.GetEncoding("UTF-8");   //解决中文乱码
    Response.ContentType = "application/x-zip-compressed";
    Response.AddHeader("Content-Disposition", "attachment;filename=" + Server.UrlEncode(fileName + info.Extension));    //解决中文文件名乱码
    //不指明Content-Length用Flush的话不会显示下载进度 
    Response.AddHeader("Content-Length", fileSize.ToString());
    Response.TransmitFile(filePath, 0, fileSize);
    Response.Flush();
    Response.Close(); 
    WebClient 以流方式下载
    string url = "http://192.168.8.53:808/test.mp3";
    
    WebClient web = new WebClient();
    byte[] arr = web.DownloadData(url);
    context.Response.ContentType = "application/octet-stream";
    context.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode("test.mp3", Encoding.UTF8));
    context.Response.AddHeader("Content-Length", arr.Length.ToString());
    context.Response.BinaryWrite(arr);
    context.Response.End();
    context.Response.Close();
  • 相关阅读:
    django模板语言导入自定html文件内容
    django模板语言中的自定义函数
    ID3决策树
    K近邻算法
    Mini-Batch 、Momentum、Adam算法的实现
    python3安装scrapy教程
    numpy高级应用
    numpy.random随机数生成
    numpy 线性代数
    numpy文件操作
  • 原文地址:https://www.cnblogs.com/xgao/p/4174035.html
Copyright © 2011-2022 走看看