从数据库中读取文件

None.gif < h3 > 从数据库中读取文件 </ h3 >
None.gif            输入文件在数据库中的ID:
None.gif            
< asp:textbox  id ="FileIDTextBox"  runat ="server" ></ asp:textbox >
None.gif            
< asp:button  id ="SubmitBtn"  runat ="server"  Text ="显示"  OnClick ="SubmitBtn_Click" ></ asp:button >

None.gif              void  SubmitBtn_Click( object  sender, System.EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif            
dot.gif {
InBlock.gif                
int FileID = Convert.ToInt32(FileIDTextBox.Text);
InBlock.gif
InBlock.gif                
string ConnStr = System.Configuration.ConfigurationSettings.AppSettings["ConnectionSqlServer"];
InBlock.gif                
string query = "SELECT * FROM FileTable WHERE FileID = @FileID";
InBlock.gif
InBlock.gif                SqlCommand myCommand 
= new SqlCommand(query, new SqlConnection(ConnStr));
InBlock.gif                myCommand.Parameters.Add(
"@FileID", SqlDbType.Int);
InBlock.gif                myCommand.Parameters[
"@FileID"].Value = FileID;
InBlock.gif                myCommand.Connection.Open();
InBlock.gif
InBlock.gif                SqlDataReader dr 
= myCommand.ExecuteReader();
InBlock.gif
InBlock.gif                
if(dr.Read())
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
                         Response.AddHeader("content-disposition","inline;filename=文件名.后缀名");
InBlock.gif                    Response.ContentType 
= (string)dr["FileContentType"];//Response.ContentType = "byte[]";
InBlock.gif                    Response.OutputStream.Write((
byte[])dr["FileData"], 0, (int)dr["FileSize"]);
                       //Response.BinaryWrite((byte[])dr["FileData"]);
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    Response.Write(
"没有这个文件的ID号");
InBlock.gif                    Response.End();
ExpandedSubBlockEnd.gif                }

InBlock.gif
InBlock.gif                dr.Close();
InBlock.gif                myCommand.Connection.Close();
ExpandedBlockEnd.gif            }

转载于:https://www.cnblogs.com/timsoft/articles/421580.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值