日期:2014-05-17  浏览次数:20477 次

FileUpload控件上传图像到sql数据库!拒绝复制党!进来有分@@@@ @@@@@@@@@@@@@@@@@
我想通过FileUpload控件上传图像到sql数据库,
用的是三层架构,下面是.cs文件的代码
Hashtable ht = new Hashtable();
ht.Add("LoginName", SqlStringFormat.GetQuotedString(TxtLoginName.Text));
ht.Add("Password", SqlStringFormat.GetQuotedString(TxtPass.Text));
ht.Add("Email", SqlStringFormat.GetQuotedString(TxtEmail.Text));
ht.Add("Address", SqlStringFormat.GetQuotedString(TxtAddress.Text));
ht.Add("QQ", SqlStringFormat.GetQuotedString(QQ.Text));
ht.Add("sex", SqlStringFormat.GetQuotedString(sextext.Text));
ht.Add("job", SqlStringFormat.GetQuotedString(jobtext.Text));
ht.Add("question", SqlStringFormat.GetQuotedString(questiontext.Text));
ht.Add("answer", SqlStringFormat.GetQuotedString(answertext.Text));
ht.Add("Photo", SqlStringFormat.GetQuotedString(image.Text));//////此行有错误啊,不知道应该怎么写好呢,别的都是正确的!!!!!!!!!!

运行之后提示

编译器错误信息: CS0117: “System.Web.UI.WebControls.FileUpload”并不包含“Text”的定义

源错误:

 

行 70: ht.Add("question", SqlStringFormat.GetQuotedString(questiontext.Text));
行 71: ht.Add("answer", SqlStringFormat.GetQuotedString(answertext.Text));
行 72: ht.Add("Photo", SqlStringFormat.GetQuotedString(image.Text));
行 73: User user = new User();
行 74: user.Add(ht);
 
那么应该怎么修改呢[color=#FF0000][/color]

------解决方案--------------------
很简单,把上传的图片保存成二进制形式,插入到数据库内
 byte[] image = (byte[])command.ExecuteScalar();
stream.Write(image, 0, image.Length);
Bitmap bitmap = new Bitmap(stream);
context.Response.ContentType = "image/jpeg";
bitmap.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
}

------解决方案--------------------
探讨

请注意我是想把图片保存到数据库里面,而不是图片路径,请不要复制粘贴!,谢谢合作!

------解决方案--------------------
获取上传文件的Stream,把它用StreamWriter写成byte[],然后作为参数传到你的 Insert语句。
并不是所有的sql参数都是字符串型的。


------解决方案--------------------
FileUpload图片存到数据库内还是路径?如果图片存数据库转二进制,如果存路径就直接存了。
------解决方案--------------------
ht.Add("Photo", image.FileBytes);


------解决方案--------------------
探讨

大神啊,我要把头像保存在数据库里面,保存路径太浪费空间啊