关于网页下载文件,使用数据流方式下载

关于文件下载,很多都是用href=‘文件地址‘,这样做是很不安全的,所以需要使用到文件流,以下代码用于下载一张图片。

     Response.BufferOutput = false;
        Response.Clear();
        Response.ContentType = "application/x-msdownload";
        Response.AppendHeader("Content-Disposition", "attachment; filename=" + "优惠券.jpg");
        Response.ContentType = "application/octstream";
        Response.CacheControl = "Private";
        System.IO.Stream stm = new System.IO.FileStream(Server.MapPath("~/uploadfiles/youhui/"+path), System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read);
        Response.AppendHeader("Content-length", stm.Length.ToString());
        System.IO.BinaryReader br = new System.IO.BinaryReader(stm);
        byte[] bytes;
        for (Int64 x = 0; x < (br.BaseStream.Length / 4096 + 1); x++)
        {
            bytes = br.ReadBytes(4096);
            Response.BinaryWrite(bytes);
            System.Threading.Thread.Sleep(5);
        }

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。