求图片上传数据库代码 完整代码.net08前台以及后台(有标注),现有图片一堆,需要存到数据库,数据库弄好了,img字段,不懂怎么存图片进去,急求代码,aspx页面。谢谢! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 http://topic.csdn.net/u/20110322/21/81e36415-7c88-4976-86d1-bebff7e6cb90.html img字段 是什么类型的啊? byte[] buffer = imageFile.FileData.get_BinaryData() as byte[];buffer 就可以插入数据库了。数据库对应应该是Binary或者Blob或者Image类型的字段。下面红色部分换成你的buffer。 using (var conn = new OleDbConnection(strConn)) { var comm = new OleDbCommand(); comm.Connection = conn; comm.CommandText = "insert into images (FileName, Data) values (?, ?)"; var p1 = new OleDbParameter(); p1.DbType = DbType.String; p1.Value = Path.GetFileName(file); comm.Parameters.Add(p1); var p2 = new OleDbParameter(); p2.DbType = DbType.Binary;p2.Value = File.ReadAllBytes(file); comm.Parameters.Add(p2); conn.Open(); comm.ExecuteNonQuery(); MessageBox.Show("Insert Successfully"); }2楼 您的代码我还是看不懂 http://topic.csdn.net/u/20110322/21/81e36415-7c88-4976-86d1-bebff7e6cb90.html Stream ms; byte[] picbyte; OpenFileDialog ofdSelectPic = new OpenFileDialog(); if (ofdSelectPic.ShowDialog() == DialogResult.OK) { if ((ms = ofdSelectPic.OpenFile()) != null) { picbyte = new byte[ms.Length]; ms.Position = 0; ms.Read(picbyte, 0, Convert.ToInt32(ms.Length)); SqlConnection conn = new SqlConnection(); conn.ConnectionString = ""; sql = "Insert into Person(Photo) values(@Image)"; SqlCommand cmd = new SqlCommand(sql, conn); cmd.Parameters.Add("@Image", SqlDbType.VarBinary); cmd.Parameters["@Image"].Value = picbyte; conn.Open(); cmd.ExecuteNonQuery(); conn.Close(); ms.Close(); } } SqlConnection conn=new SqlConnection(); conn.ConnectionString=""; string strSql=""; SqlCommand cmd=new SqlCommand(strSql,conn); conn.Open(); SqlDataReader reader=cmd.ExecuteReader(); reader.Read(); MemoryStream ms=new MemoryStream((byte[])reader["Photo"]); Image image=Image.FromStream(ms,true); reader.Close(); conn.Close(); picturebox1.Image=image; FileStream mystream=new FileStream("D:\\A.jpg",FileMode.Open,FileAccess.Read);long len=mystream.Length;mycmd.Parameters.Add("@image",SqlDbType.Image,(int)len,"picture");mycmd.Parameters["@image"].Direction=System.Data.ParameterDirection.Input;byte []box=new byte[len]; mystream.Read(box,0,(int)len);mycmd.Parameters["@image"].Value=box; 二进制图片存取if (this.FileUpload1.HasFile) { string strPath = FileUpload1.FileName; try { string extension = Path.GetExtension(File1.PostedFile.FileName).ToUpper(); string fileName = DateTime.Now.ToString("yyyyMMddhhmmss"); strPath = strPath.Substring(strPath.LastIndexOf("\\") + 1); FileUpload1.SaveAs.SaveAs(Server.MapPath("../UploadFile/")+fileName + extension); strPath = fileName + extension; using(SqlConnection conn =new SqlConnection("")) { conn .Open(); string sql = ""; SqlCommand cmd = new SqlCommand(sql, conn ); cmd.ExecuteNonQuery(); conn.Close(); } } catch() { } }// 一般保存路径二进制使用Stream s = FileUpload1.PostedFile.InputStream;Byte[] buffer= new Byte[FileUpload1.PostedFile.ContentLength];s.Read(buffer,0,FileUpload1.PostedFile.ContentLength); string strsql = "insert into Tempimage(images,names)values(@ImageData,@names)"; SqlCommand cmd= new SqlCommand(strsql, conn); cmd.Parameters.Add("@ImageData", SqlDbType.Image); cmd.Parameters.Add("@names", SqlDbType.VarChar); cmd.Parameters["@ImageData"].Value = buffer; cmd.Parameters["@names"].Value = ""; cmd.ExecuteNonQuery();//显示 Image1.ImageUrl="Photo.aspx?id="+Request.QueryString["id"];photo.aspxint Id=Request.QueryString["id"]==null?0:int.Parse(Request.QueryString["id"].ToString());using(SqlConnection conn=new SqlConnection()){conn.ConnectionString=""; string strSql="select * from A where Id='"+Id+"'"; SqlCommand cmd=new SqlCommand(strSql,conn) ;conn.Open();SqlDataReader reader=cmd.ExecuteReader();if(reader.Read()){Response.ContentType = "application/octet-stream";Response.BinaryWrite((Byte[])reader["Photo"]);}Response.End();} <tr> <td align="right" style="text-align: right; width: 135px;"> 图片上传:</td> <td align="left" style="width: 228px"> <asp:FileUpload ID="FileUpload4" runat="server" /> <input id="Txt_Sell" runat="server" class="txt" maxlength="50" tabindex="4" type="text" visible="false" /></td> </tr>//上传文件处理 private void UPFile(string time) { string FilePath = Server.MapPath("./") + "File"; HttpFileCollection HFC = HttpContext.Current.Request.Files; for (int i = 0; i < HFC.Count; i++) { HttpPostedFile HF = HFC[i]; if (HFC[i].ContentLength > 0 && HFC[i].FileName != "") { //HF.SaveAs(FilePath + "//" + System.IO.Path.GetFileName(HF.FileName)); string FName = HFC[i].FileName.Substring(HFC[i].FileName.LastIndexOf(".")); HF.SaveAs(FilePath + "//" + Convert.ToString(Convert.ToInt64(time) + i) + FName);//更改文件名 } } } swfuploadJQUERY 插件 多图上传 进度条Uploadify SQL数据库导入到本地 经过16个月开发,项目上线,请给出宝贵意见! 字符串问题 关于DataGrid中列都绑定了,而只有一个是模版列,如何将数据库的值给这个列. 关于web控件与html控件的使用及其效能 关于服务器控件开发中的疑问 TextBox如何绑定<%# DataBinder.Eval(Container.DataItem,"Title") %>呀?? 如何在动态生成的radiobutton页面中设置提交按钮 问一个查询数据库的问题,谢谢! asp怎么用js实现注册,登陆功能? 网页播放器一般是怎么解决的 关于MVC问题的问题请教大家!
下面红色部分换成你的buffer。 using (var conn = new OleDbConnection(strConn))
{
var comm = new OleDbCommand();
comm.Connection = conn;
comm.CommandText = "insert into images (FileName, Data) values (?, ?)";
var p1 = new OleDbParameter();
p1.DbType = DbType.String;
p1.Value = Path.GetFileName(file);
comm.Parameters.Add(p1);
var p2 = new OleDbParameter();
p2.DbType = DbType.Binary;
p2.Value = File.ReadAllBytes(file);
comm.Parameters.Add(p2);
conn.Open();
comm.ExecuteNonQuery();
MessageBox.Show("Insert Successfully");
}
2楼 您的代码我还是看不懂 http://topic.csdn.net/u/20110322/21/81e36415-7c88-4976-86d1-bebff7e6cb90.html
byte[] picbyte;
OpenFileDialog ofdSelectPic = new OpenFileDialog();
if (ofdSelectPic.ShowDialog() == DialogResult.OK)
{
if ((ms = ofdSelectPic.OpenFile()) != null)
{
picbyte = new byte[ms.Length];
ms.Position = 0;
ms.Read(picbyte, 0, Convert.ToInt32(ms.Length));
SqlConnection conn = new SqlConnection();
conn.ConnectionString = ""; sql = "Insert into Person(Photo) values(@Image)";
SqlCommand cmd = new SqlCommand(sql, conn); cmd.Parameters.Add("@Image", SqlDbType.VarBinary);
cmd.Parameters["@Image"].Value = picbyte; conn.Open();
cmd.ExecuteNonQuery();
conn.Close(); ms.Close();
}
} SqlConnection conn=new SqlConnection();
conn.ConnectionString="";
string strSql="";
SqlCommand cmd=new SqlCommand(strSql,conn);
conn.Open();
SqlDataReader reader=cmd.ExecuteReader();
reader.Read();
MemoryStream ms=new MemoryStream((byte[])reader["Photo"]);
Image image=Image.FromStream(ms,true);
reader.Close();
conn.Close();
picturebox1.Image=image;
FileStream mystream=new FileStream("D:\\A.jpg",FileMode.Open,FileAccess.Read);
long len=mystream.Length;
mycmd.Parameters.Add("@image",SqlDbType.Image,(int)len,"picture");
mycmd.Parameters["@image"].Direction=System.Data.ParameterDirection.Input;
byte []box=new byte[len];
mystream.Read(box,0,(int)len);
mycmd.Parameters["@image"].Value=box;
if (this.FileUpload1.HasFile)
{
string strPath = FileUpload1.FileName;
try
{
string extension = Path.GetExtension(File1.PostedFile.FileName).ToUpper();
string fileName = DateTime.Now.ToString("yyyyMMddhhmmss");
strPath = strPath.Substring(strPath.LastIndexOf("\\") + 1);
FileUpload1.SaveAs.SaveAs(Server.MapPath("../UploadFile/")+fileName + extension);
strPath = fileName + extension;
using(SqlConnection conn =new SqlConnection(""))
{
conn .Open();
string sql = "";
SqlCommand cmd = new SqlCommand(sql, conn );
cmd.ExecuteNonQuery();
conn.Close();
} }
catch()
{
}
}
// 一般保存路径二进制使用
Stream s = FileUpload1.PostedFile.InputStream;
Byte[] buffer= new Byte[FileUpload1.PostedFile.ContentLength];
s.Read(buffer,0,FileUpload1.PostedFile.ContentLength);
string strsql = "insert into Tempimage(images,names)values(@ImageData,@names)";
SqlCommand cmd= new SqlCommand(strsql, conn);
cmd.Parameters.Add("@ImageData", SqlDbType.Image);
cmd.Parameters.Add("@names", SqlDbType.VarChar);
cmd.Parameters["@ImageData"].Value = buffer;
cmd.Parameters["@names"].Value = "";
cmd.ExecuteNonQuery();//显示
Image1.ImageUrl="Photo.aspx?id="+Request.QueryString["id"];
photo.aspx
int Id=Request.QueryString["id"]==null?0:int.Parse(Request.QueryString["id"].ToString());
using(SqlConnection conn=new SqlConnection())
{
conn.ConnectionString="";
string strSql="select * from A where Id='"+Id+"'";
SqlCommand cmd=new SqlCommand(strSql,conn) ;
conn.Open();
SqlDataReader reader=cmd.ExecuteReader();
if(reader.Read())
{
Response.ContentType = "application/octet-stream";
Response.BinaryWrite((Byte[])reader["Photo"]);
}
Response.End();
}
<td align="right" style="text-align: right; width: 135px;">
图片上传:</td>
<td align="left" style="width: 228px">
<asp:FileUpload ID="FileUpload4" runat="server" />
<input id="Txt_Sell" runat="server" class="txt" maxlength="50" tabindex="4" type="text" visible="false" /></td>
</tr>
//上传文件处理
private void UPFile(string time)
{
string FilePath = Server.MapPath("./") + "File";
HttpFileCollection HFC = HttpContext.Current.Request.Files;
for (int i = 0; i < HFC.Count; i++)
{
HttpPostedFile HF = HFC[i];
if (HFC[i].ContentLength > 0 && HFC[i].FileName != "")
{
//HF.SaveAs(FilePath + "//" + System.IO.Path.GetFileName(HF.FileName));
string FName = HFC[i].FileName.Substring(HFC[i].FileName.LastIndexOf("."));
HF.SaveAs(FilePath + "//" + Convert.ToString(Convert.ToInt64(time) + i) + FName);//更改文件名
}
}
}