zoukankan      html  css  js  c++  java
  • Asp.Net 文件下载1——流下载(适用于大文件且防盗链)(转)

    使用流防盗链下载大文件

    直接上 Asp.net 后置代码好了

    using System;  
    using System.Data;  
    using System.Configuration;  
    using System.Collections;  
    using System.Web;  
    using System.Web.Security;  
    using System.Web.UI;  
    using System.Web.UI.WebControls;  
    using System.Web.UI.WebControls.WebParts;  
    using System.Web.UI.HtmlControls;  
    using System.IO;  
      
    public partial class FileDownload_Default : System.Web.UI.Page  
    {  
        protected void Page_Load(object sender, EventArgs e)  
        {  
      
        }  
      
        //以流方式下载文件,适合大文件  
        protected void btn_down_Click(object sender, EventArgs e)  
        {  
            try  
            {  
                //设置文件名  
                Response.AddHeader("Content-Disposition", "attachment; filename=test.jpg");  
                Response.ContentType = "application/octet-stream";  
      
                //设置下载文件的服务器路径  
                String filePath = "~/FileDownload/test.jpg";  
              
                //输出到流  
                FileStream stream = new FileStream(Server.MapPath(filePath), FileMode.Open, FileAccess.Read, FileShare.Read);  
                BinaryReader reader  = new BinaryReader (stream);  
      
                Response.Clear();  
                while (true)  
                {  
                    byte [] bt = reader.ReadBytes(1000);  
                    Response.OutputStream.Write(bt,0,1000);  
                    if(bt.Length<1000)  
                        break;  
                }  
                reader.Close();  
                stream.Dispose();  
                Response.End();  
            }  
            catch(Exception ex)  
            {  
                this.ClientScript.RegisterStartupScript(this.GetType(),"new","<mce:script type="text/javascript"><!--  
    alert('下载文件异常 : "+ex.Message+"');  
    // --></mce:script>");  
            }  
        }  
    }  
  • 相关阅读:
    Python爬虫之-动态网页数据抓取
    Python爬虫之 正则表达式和re模块
    Python爬虫 XPath语法和lxml模块
    Python 多线程爬虫
    PAT 1037 在霍格沃茨找零钱
    PAT 1033 旧键盘打字
    PAT 1019 数字黑洞
    PAT 1057 数零壹
    PAT 1026 程序运行时间
    PAT 1023 组个最小数
  • 原文地址:https://www.cnblogs.com/flyhigh1860/p/3231374.html
Copyright © 2011-2022 走看看