zoukankan      html  css  js  c++  java
  • 将图片以二进制代码形式存入数据库

    上传aspx
    <!--上传图片-->
                <input id="UpPhoto" name="UpPhoto" runat="server" type="file">
                <asp:Button id="btnAdd" name="btnAdd" runat="server" Text="上传" 
                    onclick="btnAdd_Click"></asp:Button>








    上传aspx.cs
     string connstr = @"Data Source=WANGRUI-PC\WANGRUI;Initial Catalog=JingDong;Persist Security Info=True;User ID=sa;Password=admin";
                using (SqlConnection conn = new SqlConnection(connstr))
                {
                    conn.Open();
                    using (SqlCommand cmd = conn.CreateCommand())
                    {
                        //获得图象并把图象转换为byte[] 
                        HttpPostedFile upPhoto = UpPhoto.PostedFile;
                        int upPhotoLength = upPhoto.ContentLength;
                        byte[] PhotoArray = new Byte[upPhotoLength];
                        Stream PhotoStream = upPhoto.InputStream;
                        PhotoStream.Read(PhotoArray, 0, upPhotoLength);
                        cmd.CommandText = "insert into eBookxinxi(eBimg,eBmimg,eBtj,eBnjj,eBzjj,eBml,eBname,sjid,eBzz,eBjg) values (@FImage,@image,@tj,@njj,@zjj,@ml,@name,@id,@zz,@jg)";
                        cmd.Parameters.Add("@FImage", SqlDbType.Image);
                        cmd.Parameters["@FImage"].Value = PhotoArray;


    p'


    建一个图片网页
     if (!Page.IsPostBack)
                {
                    SqlConnection conn = new SqlConnection();
                    conn.ConnectionString = "Data Source=WANGRUI-PC\\WANGRUI;Database=JingDong;User Id=sa;Pwd=admin";
                    string strSql = "select * from eBookxinxi where eBid=1";//这里假设获取id为2的图片 
                    SqlCommand cmd = new SqlCommand(strSql, conn);
                    conn.Open();
                    SqlDataReader reader = cmd.ExecuteReader();
                    reader.Read();
                    Response.ContentType = "application/octet-stream";
                    Response.BinaryWrite((Byte[])reader["eBimg"]);
                    Response.End();
                    reader.Close();
                }  
  • 相关阅读:
    仿酷狗音乐播放器开发日志二十七 用ole为窗体增加文件拖动功能(附源码)
    redis持久化和主从同步
    MySQL主从复制
    Nginx 安装与详解
    ContOS安装配置MySQL,redis
    ContOS7编译安装python3,配置虚拟环境
    ContOS7切换国内源
    ContOS 常用命令
    轮询、长轮询、websock
    flask之三方组件
  • 原文地址:https://www.cnblogs.com/qiqiBoKe/p/2791556.html
Copyright © 2011-2022 走看看