ataSet ds = new DataSet();
sda.Fill(ds);
this.DataGrid1.DataSource = ds.Tables[0].DefaultView;
this.DataBind();
}
protected void DataGrid1_ItemCommand(object source, DataGridCommandEventArgs e)
{
int fileID = Convert.ToInt32(e.CommandArgument.ToString());
SqlConnection con = new SqlConnection("server=zhan;database=download;uid=sa;pwd=zhan");
con.Open();
string sql = "select * from study where fileID=''" + fileID + "''";
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["fileName"].ToString()));
byte[] file = (Byte[])sdr["fileData"];//读出数据
int a = file.Length;
Response.BinaryWrite(file);
Response.Flush();
Response.End();
sdr.Close();
con.Close();
}
protected void btnDownLoad_Click(object sender, EventArgs e)
{
}
}
SQL数据库:
create database download
use download
create table study
(
fileID int identity(1,1)primary key,
fileName varchar(200),
filePath varchar(300),
fileType varchar(50),
fileData image
)
create procedure insertFile--存储过程
@fileName varchar(200),
@filePath varchar(300),
@fileType varchar(50),
@fileData image
as
insert into study(fileName,filePath,fileType,fileData)
values(@fileName,@filePath,@fileType,@fileData)
|