如何从数据库中读出并下载文件

using System;
using System.Data;
using System.Collections;
using System.Configuration;
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.Data.SqlClient;

using System.IO;
//using System.IO.FileStream;
public partial class _office_down : System.Web.UI.Page
{
public string strInfo;
public string strlx;

 protected void Page_Load(object sender, EventArgs e)
    {
  if (!IsPostBack)
        {
  Download();
  //upload();
  }
 }
//定义全局变量,供前台JAVASCRIPT调用。
public   string   giveinfoback()  
  {  
         //string   strInfo="1";//不要以下条件,自身赋值测试
    string   strInfo;
    if (Request["id"]==null)
    {
         strInfo=Session["strInfo"].ToString().Trim();   //从会话全局变量中取值,按保存按钮后,是无法取得request[]值的。
    }
    else
    {
   
    strInfo=Request["id"].ToString().Trim();   //取得打开文件的传值
    Session["strInfo"]=strInfo;                //赋值会话全局变量,按保存按钮后重新打开文件使用,否则报错
    
          }
    return   strInfo;  
  }  
 
 public   string   givelxback()  
  {  
         //string   strInfo="1";//不要以下条件,自身赋值测试
    string   strlx;
    if (Request["n"]==null)
    {
         strlx=Session["strlx"].ToString().Trim();   //从会话全局变量中取值,按保存按钮后,是无法取得request[]值的。
    }
    else
    {
   
    strlx=Request["n"].ToString().Trim();   //取得类型
   
   
    Session["strlx"]=strlx;                //赋值会话全局变量,按保存按钮后重新打开文件使用,否则报错
    
          }
    return   strlx;  
  }  
 
 

//
private bool Download()
{
    bool result = false;
   
 string pid =Request["id"].ToString().Trim();  //取得要下载记录关键字
    string sql = @"select  *  from table_word where sdate='"+pid+"'";
     SqlConnection conn = new SqlConnection("Data Source= (local);Persist Security Info=True;Database=office;Uid=sa;Pwd=");

    conn.Open();
    SqlCommand cmd = new SqlCommand(sql, conn);
    SqlDataReader reader = cmd.ExecuteReader();
   
    if (reader.Read())
    {
 string wjlx;

//文件名取记录标题+文件类型
 string filename1=reader["bt"].ToString().Trim()+"."+reader["wjlx"].ToString().Trim();
 switch( reader["wjlx"].ToString().Trim())
 {
 case "doc":
   wjlx="ms-word";
   break;
 case "xls":
   wjlx="ms-excel";
   break;
 case "ppt":
   wjlx="ms-powerpoint";
   break;
   default:
   wjlx="ms-word";
   break;
 }
        Response.Clear();
        Response.Buffer = true;
        Response.Charset = "utf-8";
        Response.AppendHeader("Content-Disposition", "attachment;filename="+filename1); //把 attachment 改为 online 则在线打开
        Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
       // Response.ContentType = "application/ms-excel";
           
          Response.ContentType = "application/"+wjlx;
    
        Page.EnableViewState = false;
        Response.BinaryWrite((byte[])reader["filedata"]);
        result = true;
    }
   
    reader.Close();
    reader.Dispose();
    cmd.Dispose();
    conn.Close();
    conn.Dispose();
   
    return result;
}
//
  }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值