怎样下载保存到数据库的image字段中的文件
怎样下载保存到数据库的image字段中的文件
问题点数:20、回复次数:6Top
1 楼zbyh331(我才刚上路耶!)回复于 2005-12-16 11:51:10 得分 0
http://www.cnblogs.com/kwklover/archive/2004/06/22/17799.aspxTop
2 楼zbyh331(我才刚上路耶!)回复于 2005-12-16 11:51:32 得分 0
Asp.Net在SqlServer中的图片存取技术
在使用asp.net将图片上传并存入SqlServer中,然后从SqlServer中读取并显示出来
一,上传并存入SqlServer
数据库结构
create table test
{
id identity(1,1),
FImage image
}
相关的存储过程
Create proc UpdateImage
(
@UpdateImage Image
)
As
Insert Into test(FImage) values(@UpdateImage)
GO
在UpPhoto.aspx文件中添加如下:
<input id="UpPhoto" name="UpPhoto" runat="server" type="file">
<asp:Button id="btnAdd" name="btnAdd" runat="server" Text="上传"></asp:Button>
然后在后置代码文件UpPhoto.aspx.cs添加btnAdd按钮的单击事件处理代码:
private void btnAdd_Click(object sender, System.EventArgs e)
{
//获得图象并把图象转换为byte[]
HttpPostedFile upPhoto=UpPhoto.PostedFile;
int upPhotoLength=upPhoto.ContentLength;
byte[] PhotoArray=new Byte[upPhotoLength];
Stream PhotoStream=upPhoto.InputStream;
PhotoStream.Read(PhotoArray,0,upPhotoLength);
//连接数据库
SqlConnection conn=new SqlConnection();
conn.ConnectionString="Data Source=localhost;Database=test;User Id=sa;Pwd=sa";
SqlCommand cmd=new SqlCommand("UpdateImage",conn);
cmd.CommandType=CommandType.StoredProcedure;
cmd.Parameters.Add("@UpdateImage",SqlDbType.Image);
cmd.Parameters["@UpdateImage"].Value=PhotoArray;
//如果你希望不使用存储过程来添加图片把上面四句代码改为:
//string strSql="Insert into test(FImage) values(@FImage)";
//SqlCommand cmd=new SqlCommand(strSql,conn);
//cmd.Parameters.Add("@FImage",SqlDbType.Image);
//cmd.Parameters["@FImage"].Value=PhotoArray;
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}
二,从SqlServer中读取并显示出来
在需要显示图片的地方添加如下代码:
<asp:image id="imgPhoto" runat="server" ImageUrl="ShowPhoto.aspx"></asp:image>
ShowPhoto.aspx主体代码:
private void Page_Load(object sender, System.EventArgs e)
{
if(!Page.IsPostBack)
{
SqlConnection conn=new SqlConnection()
conn.ConnectionString="Data Source=localhost;Database=test;User Id=sa;Pwd=sa";
string strSql="select * from test where id=2";//这里假设获取id为2的图片
SqlCommand cmd=new SqlCommand()
reader.Read();
Response.ContentType="application/octet-stream";
Response.BinaryWrite((Byte[])reader["FImage"]);
Response.End();
reader.Close();
}
}
Top
3 楼wjjdnajj(色空)回复于 2005-12-16 12:30:53 得分 0
我用的是vs2005,上面的代码运行后得不想要的效果啊,我要的是出现一个下载框下载文件Top
4 楼wjjdnajj(色空)回复于 2005-12-16 15:25:35 得分 0
在网上搜了很久,发现都没有很好的解决办法啊!Top
5 楼wjjdnajj(色空)回复于 2005-12-16 17:43:51 得分 0
顶一次就去周末了,哈哈Top
6 楼lzdjoa(西边)回复于 2005-12-19 08:37:31 得分 20
要这样做:
if (DR["fdata"] != null)
{
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename="+DR["fname"].ToString()//这一句是关键
Response.ContentType =DR["ftype"].ToString();
Response.BinaryWrite((byte[])DR["fdata"]);
Response.End();
}Top
相关问题
- 怎样将数据库中image字段保存到xml中?
- 在数据库中保存bmp字段
- sybase 数据库的image字段与sql server中的image字段,写的方法一样,但sybase 可以保存上,sql server的保存不上。
- 显示保存在数据库中image字段中的WORD文档,ie总要问我是"保存 " "打开" ?代码见内
- ===用TQuery能读取数据库中的保存Jpeg图像的字段吗?===
- 将文件保存到数据库的ole字段中的问题!
- 请问如何将计算字段保存到数据库里面?
- 自动计算字段的数值如何保存回数据库!
- 请问:怎样将图片(Bmp,Jpeg...)保存入数据库的ole字段中
- 请问access数据库保存图片,用什么字段类型?




