我的文件是以二进制形式上传到数据库
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.IO;
using System.Configuration;
using System.Collections;
using System.Web.Security;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class chakan : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.databindTodg();
}
}
private void databindTodg()
{
SqlConnection con = new SqlConnection(@"data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\TS.mdf;User Instance=true");
con.Open();
string sql = @"select * from Documents ";//string sql = "select * from Documents";
SqlDataAdapter sda = new SqlDataAdapter(sql, con);
DataSet ds = new DataSet();
sda.Fill(ds);
this.GridView1.DataSource = ds.Tables[0].DefaultView;
GridView1.DataSourceID = null;
GridView1.DataSource = ds;
this.DataBind();
}
protected void lbtnDownLoad_Command(object sender, CommandEventArgs e)
{
int SNo = Convert.ToInt32(e.CommandArgument.ToString());
SqlConnection con = new SqlConnection(@"data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\TS.mdf;User Instance=true");
con.Open();
string sql = "select * from Documents where SNo=" + SNo + "";
SqlCommand cmd = new SqlCommand(sql, con);
SqlDataReader sdr = cmd.ExecuteReader();
sdr.Read();
Response.Buffer = true;
Page.Response.Clear();//清除缓冲区所有内容
Page.Response.ContentType = "application/octet-stream";
Page.Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(sdr["Name_File"].ToString()));
byte[] file = (Byte[])sdr["fileData"];//读出数据
int a = file.Length;
Response.BinaryWrite(file);
Response.Flush();
Response.End();
sdr.Close();
con.Close();