将图片传入sql数据库时碰到的问题 必须是在提交后才能得到File1.PostedFile。应该是你的代码有问题。把相关的代码都贴出来吧。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 if(FileUp.PostedFile!=null) { string myFilename=FileUp.PostedFile.FileName; int posi=myFilename.LastIndexOf("\\"); string fn=myFilename.Substring(posi); string spath=Server.MapPath(@"./zp/"); FileUp.PostedFile.SaveAs(spath+fn); this.Img_zp.ImageUrl =@"./zp/"+fn; this.Button1.Enabled =false; UpZp=true; } 我的代码,用于http://www.hntbtc.com/rsgl/rsgl.aspx源程序http://www.hntbtc.com/hncjgl 真的活不成了,if(FileUp.PostedFile!=null) 果然判断出来了。就是这里过不去,可是就这样,我的程序另外新建一个项目,把相同的代码复制过去,却可以入库。是不是哪儿没设置对?代码应该没问题吧:private void Button1_Click(object sender, System.EventArgs e){ Stream imgStream; int imgLen; if (UploadFile.PostedFile !=null) { imgStream = UploadFile.PostedFile.InputStream; imgLen = UploadFile.PostedFile.ContentLength; byte[] imgBinaryData=new byte[imgLen]; int n = imgStream.Read(imgBinaryData, 0, imgLen); int NumRowsAffected = MyDatabaseMethod( imgBinaryData); }}public int MyDatabaseMethod(byte[] imgbin){SqlConnection connection = new SqlConnection("Initial Catalog=basedb;Data Source=zdf;Integrated Security=SSPI;");string SQL="INSERT INTO MinuteInfo (ResultPic) VALUES (@img_data) ";SqlCommand command=new SqlCommand ( SQL,connection ); SqlParameter param1=new SqlParameter("@img_data",SqlDbType.Image ); param1.Value = imgbin; command.Parameters.Add( param1 ); connection.Open(); int numRowsAffected = command.ExecuteNonQuery(); connection.Close(); return numRowsAffected;} 谢谢2位,原因找到了,表单里缺少 enctype="multipart/form-data"十分感谢2位的支持。 一个判断的简单为题 运行ASP.NET站,应用程序池,应当怎样设置? "以下文件中的行尾不一致,要将行尾标准化吗"是怎么回事? 一个高难度的存储过程 这错误啥意思:??? 成员名称不能与它们的封闭类型相同,请举例说明一下 水晶报表分组排序的问题 关于单一页面访问次数统计的问题。 asp.net 文件上传时遇到的诡异问题 如何以独占方式打开ACCESS数据库 datagrid中templatecolumn中的控件事件问题,谢谢帮忙》》》 如何获取客户端显示器的分辨率? 我想做<电子相册>就像.http://www.sonicalbum.com/这种.祥情请看贴子
{
string myFilename=FileUp.PostedFile.FileName;
int posi=myFilename.LastIndexOf("\\");
string fn=myFilename.Substring(posi);
string spath=Server.MapPath(@"./zp/");
FileUp.PostedFile.SaveAs(spath+fn);
this.Img_zp.ImageUrl =@"./zp/"+fn;
this.Button1.Enabled =false;
UpZp=true;
}
我的代码,
用于
http://www.hntbtc.com/rsgl/rsgl.aspx
源程序
http://www.hntbtc.com/hncjgl
{
Stream imgStream;
int imgLen;
if (UploadFile.PostedFile !=null)
{ imgStream = UploadFile.PostedFile.InputStream;
imgLen = UploadFile.PostedFile.ContentLength;
byte[] imgBinaryData=new byte[imgLen];
int n = imgStream.Read(imgBinaryData, 0, imgLen);
int NumRowsAffected = MyDatabaseMethod( imgBinaryData);
}
}
public int MyDatabaseMethod(byte[] imgbin)
{
SqlConnection connection = new SqlConnection("Initial Catalog=basedb;Data Source=zdf;Integrated Security=SSPI;");
string SQL="INSERT INTO MinuteInfo (ResultPic) VALUES (@img_data) ";
SqlCommand command=new SqlCommand ( SQL,connection );
SqlParameter param1=new SqlParameter("@img_data",SqlDbType.Image );
param1.Value = imgbin;
command.Parameters.Add( param1 ); connection.Open();
int numRowsAffected = command.ExecuteNonQuery();
connection.Close();
return numRowsAffected;
}
十分感谢2位的支持。