在asp.net中如何下载从sql数据库调出来的文件?要详细代码和注释,尤其是从数据库调的代码
protectedvoidButton3_Click(objectsender,EventArgse){stringid=Request["id"].ToString()...
protected void Button3_Click(object sender, EventArgs e)
{
string id = Request["id"].ToString();
SqlConnection cn = new SqlConnection("server=localhost;uid=sa;pwd=1234;database=data");
cn.Open();
string sql = "select * from ziliao where id="+id;
SqlCommand db=new SqlCommand(sql,cn);
SqlDataReader reader = db.ExecuteReader();
string fname = "";
if (reader.Read())
{
//reader.Read();
fname = reader["title"].ToString();
}
string fileName =fname;//客户端保存的文件名
string filePath = Server.MapPath("upfile/"+fname);//路径
System.IO.FileInfo fileInfo = new System.IO.FileInfo(filePath);
if (fileInfo.Exists == true)
{
const long ChunkSize = 102400;//100K 每次读取文件,只读取100K,这样可以缓解服务器的压力
byte[] buffer = new byte[ChunkSize];
Response.Clear();
System.IO.FileStream iStream = System.IO.File.OpenRead(filePath);
long dataLengthToRead = iStream.Length;//获取下载的文件总大小
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName));
while (dataLengthToRead > 0 && Response.IsClientConnected)
{
int lengthRead = iStream.Read(buffer, 0, Convert.ToInt32(ChunkSize));//读取的大小
Response.OutputStream.Write(buffer, 0, lengthRead);
Response.Flush();
dataLengthToRead = dataLengthToRead - lengthRead;
}
Response.Close();
}
这段代码不好使,麻烦看看是什么原因吧!谢谢啦! 展开
{
string id = Request["id"].ToString();
SqlConnection cn = new SqlConnection("server=localhost;uid=sa;pwd=1234;database=data");
cn.Open();
string sql = "select * from ziliao where id="+id;
SqlCommand db=new SqlCommand(sql,cn);
SqlDataReader reader = db.ExecuteReader();
string fname = "";
if (reader.Read())
{
//reader.Read();
fname = reader["title"].ToString();
}
string fileName =fname;//客户端保存的文件名
string filePath = Server.MapPath("upfile/"+fname);//路径
System.IO.FileInfo fileInfo = new System.IO.FileInfo(filePath);
if (fileInfo.Exists == true)
{
const long ChunkSize = 102400;//100K 每次读取文件,只读取100K,这样可以缓解服务器的压力
byte[] buffer = new byte[ChunkSize];
Response.Clear();
System.IO.FileStream iStream = System.IO.File.OpenRead(filePath);
long dataLengthToRead = iStream.Length;//获取下载的文件总大小
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName));
while (dataLengthToRead > 0 && Response.IsClientConnected)
{
int lengthRead = iStream.Read(buffer, 0, Convert.ToInt32(ChunkSize));//读取的大小
Response.OutputStream.Write(buffer, 0, lengthRead);
Response.Flush();
dataLengthToRead = dataLengthToRead - lengthRead;
}
Response.Close();
}
这段代码不好使,麻烦看看是什么原因吧!谢谢啦! 展开
4个回答
展开全部
我做过读取二进制图片 楼主可以参考下
string str = "server=DA5A491B3F7544B;database=study;uid=sa;pwd=liyi123";
SqlConnection con = new SqlConnection(str);
con.Open();
string sql = "select * from picture where [id]=2";
SqlCommand cmd=new SqlCommand(sql,con);
SqlDataReader sdr = cmd.ExecuteReader();
if(sdr.HasRows)
{
sdr.Read();
byte[] bytes = (byte[]) sdr["pic"];
Response.ContentType = "image/jpg";
Response.OutputStream.Write(bytes,0,bytes.Length);
Response.End();
}
sdr.Close();
con.Close();
string str = "server=DA5A491B3F7544B;database=study;uid=sa;pwd=liyi123";
SqlConnection con = new SqlConnection(str);
con.Open();
string sql = "select * from picture where [id]=2";
SqlCommand cmd=new SqlCommand(sql,con);
SqlDataReader sdr = cmd.ExecuteReader();
if(sdr.HasRows)
{
sdr.Read();
byte[] bytes = (byte[]) sdr["pic"];
Response.ContentType = "image/jpg";
Response.OutputStream.Write(bytes,0,bytes.Length);
Response.End();
}
sdr.Close();
con.Close();
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
这段代码太麻烦,过于繁琐,而且容易出错,建议换一种方法!
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
我看了你的代码,你是把文件名存你数据库的,要下载的话就这样
<a href="upfile/"+fname">下载</a>这样就行了啊
<a href="upfile/"+fname">下载</a>这样就行了啊
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
二进制流 直接输出到客服端,我想你是把文件二进制存到数据库吧
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询