C# 普通下载

C# 实现下载功能:(数据库中存放的数据为  varbinary() 类型数据)

          byte[] by = null;  //定义一个字节数组用来存放数据库里读取到的字节数

   

   if (dt.Rows[0]["Byte"] != null || dt.Rows[0]["Byte"] != DBNull.Value)   //判断是否在数据库中找到  数据
           {
                   by = (byte[])dt.Rows[0]["Byte"];     ///将数据转换为  字节类型  赋值给 by
           }
           else
           {
                    stre.Byte = null;
                    return Content("<script>alert('下载失败!');location.href='/Home/Index';</script>");
           }

 

           Response.Clear();                //清除缓冲区流中的所有内容输出
           Response.ClearHeaders();         //清除缓冲区流中的所有头,不知道为什么,不写这句会显示错误页面
           Response.Buffer = false;        //设置缓冲输出为false
           Response.ContentType = "application/" + stre.Extension;
           Response.AddHeader("Content-Disposition", "inline;FileName=" + stre.FileName);
           Response.BinaryWrite(by);
           Response.Flush();       //向客户端发送当前所有缓冲的输出
           Response.Close();

 

           //return Content("<script>alert('下载成功!');location.href='/Home/Index';</script>");

posted @ 2017-09-15 15:41  Neuropathy  阅读(187)  评论(0编辑  收藏  举报