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;
}
//
}