前端时间做视频智能监控,第三方自动拍照时保存至数据库,以二进制存入。 为了团队更方便后续关于此功能的开发。
在此做下简易的总结:1. 数据库里 用 varbinary(MAX) , PerHomeLogo
2. 写入到数据库时,如下文示例代码
3. 读取时,如果winform就更简单了。连路径都不用转换了.
using System;
using System.Collections; using System.Configuration; using System.Data; using System.Data.SqlClient; using System.IO; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; public partial class imgToDb : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void ImageButton1_Click(object sender, ImageClickEventArgs e) {//写入数据库 string PerHomeName = tbPerHomeName.Text; string PerHomeSign = txtPerSign.Text; string imaPath = uploadFile.PostedFile.FileName; string lastName = imaPath.Substring(imaPath.LastIndexOf(".") + 1); if (uploadFile.PostedFile.FileName != "" && lastName.ToLower() == "jpg" || lastName.ToLower() == "gif" || lastName.ToLower() == "bmp" || lastName.ToLower() == "png") { if (uploadFile.PostedFile.ContentLength > 40960000) { Response.Write("<script language = 'javascript'> alert('你上传的图片超过了40KB!')</script>"); return; } int imaLength = uploadFile.PostedFile.ContentLength; Byte[] imageData = new Byte[imaLength]; HttpPostedFile hp = uploadFile.PostedFile; Stream imageStream = hp.InputStream; imageStream.Read(imageData, 0, imaLength); //=============== string sql = "insert into PerHomeDetail(PerHomeName,PerHomeSign,PerHomeLogo) values(@PerHomeName,@PerHomeSign,@PerHomeLogo); select @@identity ;"; int idx = Convert.ToInt32(SqlHelper.ExecuteScalar(CommandType.Text, sql , new SqlParameter("@PerHomeName", PerHomeName) , new SqlParameter("@PerHomeSign", PerHomeSign) , new SqlParameter("@PerHomeLogo", imageData) )); Response.Write("<script>alert('保存个人信息')</script>"); } else { Response.Write("<script>alert('上传头像不能为空,且格式必须为gif或jpg!');location = 'javascript:HistoryEventArgs.go(-1)'</script>"); } } protected void Button1_Click(object sender, EventArgs e) {//读取 DBEntity.PerHomeDetail ent = new DBEntity.PerHomeDetail(); ent = ent.Get("1"); byte[] bytes = (byte[])ent.PerHomeLogo; MemoryStream ms = new MemoryStream(bytes); System.Drawing.Bitmap img = (System.Drawing.Bitmap)System.Drawing.Bitmap.FromStream(ms, false); //转换成Bitmap //图片路径 string strPath = "~/upload/wangwu.JPG"; string strPhotoPath = Server.MapPath(strPath); //保存图片文件 BinaryWriter bw = new BinaryWriter(File.Open(strPhotoPath,FileMode.OpenOrCreate)); bw.Write(bytes); bw.Close(); this.Image1.ImageUrl = strPath; } }
前端页面
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="imgToDb.aspx.cs" Inherits="imgToDb" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>无标题页</title> <style type="text/css"> .auto-style1 { width: 313px; } </style> </head> <body> <form id="form1" runat="server"> <div> </div> <table style="width:100%;"> <tr> <td class="auto-style1"> 个性空间名 : </td> <td> <asp:TextBox ID="tbPerHomeName" runat="server"></asp:TextBox> </td> <td> </td> <td> </td> </tr> <tr> <td class="auto-style1"> </td> <td> </td> <td> </td> <td> </td> </tr> <tr> <td class="auto-style1"> 个性头像:</td> <td> <asp:FileUpload ID="uploadFile" runat="server" /> </td> <td> </td> <td> </td> </tr> <tr> <td class="auto-style1"> 个性签名: </td> <td> <asp:TextBox ID="txtPerSign" runat="server" Height="99px"></asp:TextBox> </td> <td> </td> <td> </td> </tr> <tr> <td id="tbPerHomeName" class="auto-style1"> </td> <td id="tbPerHomeName"> <asp:ImageButton ID="ImageButton1" runat="server" Height="20px" οnclick="ImageButton1_Click" Width="196px" /> <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" /> <br /> <asp:Image ID="Image1" runat="server" Height="139px" Width="157px" /> </td> <td> </td> <td> </td> </tr> </table> </form> </body> </html>
小实体类
public partial class PerHomeDetail
public System.Int32? idx { get; set; } // public System.String PerHomeName { get; set; } // public System.String PerHomeSign { get; set; } // public System.Object PerHomeLogo { get; set; } //