码迷,mamicode.com
首页 > Web开发 > 详细

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

时间:2014-12-03 23:05:47      阅读:201      评论:0      收藏:0      [点我收藏+]

标签:style   blog   io   ar   color   os   使用   sp   for   

关于文件下载,很多都是用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);
        }

 

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

标签:style   blog   io   ar   color   os   使用   sp   for   

原文地址:http://www.cnblogs.com/weihui2013/p/4141290.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!