asp.net2.0 fileupload

lixiang4024 2007-02-02 10:32:22
asp.net2.0 (c#)fileupload能将图片存储到数据库吗?

我昨天找了晚上二点的资料,论坛的关键字都找了"fileupload" "上传"基本上都看了,但还是不懂.

找了一些资料,但是好像不适合2.0的fileupload.

下面的资料,我在上传文件时报错.

顺例问一下2.0也是用这个代码吗?

Stream imgdatastream = File1.PostedFile.InputStream;//未将对象引用设置到对象的实例。
   int imgdatalen = File1.PostedFile.ContentLength;
   string imgtype = File1.PostedFile.ContentType;
   string imgtitle = TextBox1.Text;
   byte[] imgdata = new byte[imgdatalen];
   int n = imgdatastream.Read(imgdata,0,imgdatalen);
   string connstr=
   ((NameValueCollection)Context.GetConfig
   ("appSettings"))["connstr"];
   SqlConnection connection = new SqlConnection(connstr);
   SqlCommand command = new SqlCommand
   ("INSERT INTO ImageStore(imgtitle,imgtype,imgdata)
   VALUES ( @imgtitle, @imgtype,@imgdata )", connection );
   SqlParameter paramTitle = new SqlParameter
   ("@imgtitle", SqlDbType.VarChar,50 );
   paramTitle.Value = imgtitle;
   command.Parameters.Add( paramTitle);
   SqlParameter paramData = new SqlParameter
   ( "@imgdata", SqlDbType.Image );
   paramData.Value = imgdata;
   command.Parameters.Add( paramData );
   SqlParameter paramType = new SqlParameter
   ( "@imgtype", SqlDbType.VarChar,50 );
   paramType.Value = imgtype;
   command.Parameters.Add( paramType );
   connection.Open();
   int numRowsAffected = command.ExecuteNonQuery();
   connection.Close();


private void Page_Load(object sender, System.EventArgs e)
   {
   string imgid =Request.QueryString["imgid"];
   string connstr=((NameValueCollection)
   Context.GetConfig("appSettings"))["connstr"];
   string sql="SELECT imgdata, imgtype FROM ImageStore WHERE id = "
   + imgid;
   SqlConnection connection = new SqlConnection(connstr);
   SqlCommand command = new SqlCommand(sql, connection);
   connection.Open();
   SqlDataReader dr = command.ExecuteReader();
   if(dr.Read())
   {
   Response.ContentType = dr["imgtype"].ToString();
   Response.BinaryWrite( (byte[]) dr["imgdata"] );
   }
   connection.Close();
   }
...全文
202 2 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
hunanchina 2007-02-11
  • 打赏
  • 举报
回复
用gridview 加模板,同时注意路径
flyin2006 2007-02-02
  • 打赏
  • 举报
回复
基本是啊

62,241

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧