图片的数据库存取

系统 1863 0

  SQL Server数据库里图片以二进制格式的image类型存储,存入时先转换成二进制数据,取出时由二进制转换成image/jpg格式才能显示

存入:

视图代码,需要一个上传文件的控件

      @using (Html.BeginForm(
      
        "
      
      
        Index
      
      
        "
      
      , 
      
        "
      
      
        NewImg
      
      
        "
      
      , FormMethod.Post, 
      
        new
      
       { enctype = 
      
        "
      
      
        multipart/form-data
      
      
        "
      
      
         }))

{

    
      
      
        if
      
       (@ViewBag.ImgId != 
      
        null
      
      
        )

    {

        
      
      <img src=
      
        "
      
      
        @Url.Action(
      
      
        "
      
      ShowImg
      
        "
      
      
        , 
      
      
        "
      
      Image
      
        "
      
      
        , new { id = ViewBag.ImgId })
      
      
        "
      
       width=
      
        "
      
      
        111
      
      
        "
      
       height=
      
        "
      
      
        111
      
      
        "
      
       alt=
      
        "
      
      
        img
      
      
        "
      
       />

        <br />

        <br />
      
        

    }

    
      
      <input type=
      
        "
      
      
        file
      
      
        "
      
       name=
      
        "
      
      
        imgUserProfile
      
      
        "
      
       id=
      
        "
      
      
        imgUserProfile
      
      
        "
      
       />

    <br />

    <br />

    <input type=
      
        "
      
      
        submit
      
      
        "
      
       value=
      
        "
      
      
        upload img
      
      
        "
      
       />
      
        

}
      
    

在控制器中,接收到传入的图片文件,以byte类型存入数据库

      
        public
      
      
         ActionResult Index(HttpPostedFileBase imgUserProfile)

        {

            
      
      
        try
      
      
        

            {

                
      
      
        using
      
       (
      
        var
      
       context = 
      
        new
      
      
         newpicEntities())

                {

                    
      
      
        var
      
       imgData = 
      
        new
      
       pict1();
      
        //


      
      
        var
      
       imgLength =
      
         imgUserProfile.ContentLength;



                    
      
      
        var
      
       imgByte = 
      
        new
      
      
        byte
      
      
        [imgLength];



                    imgUserProfile.InputStream.Read(imgByte, 
      
      
        0
      
      
        , imgLength);



                    imgData.pict 
      
      =
      
         imgByte;



                    context.AddTopict1(imgData);

                    context.SaveChanges();

                    ViewBag.ImgId 
      
      =
      
         imgData.id;

                    ViewBag.Result 
      
      = 
      
        "
      
      
        success
      
      
        "
      
      
        ;

                }

            }

            
      
      
        catch
      
      
         (Exception e)

            {

                ViewBag.Result 
      
      =
      
         e;

            }

            
      
      
        return
      
       View(
      
        "
      
      
        Index
      
      
        "
      
      
        );

        }
      
    

读取并显示数据库中图片:

控制器

      
        public
      
       ActionResult ShowImg(
      
        int
      
      
         id)

        {

            
      
      
        var
      
       image = (
      
        from
      
       m 
      
        in
      
      
         db.pict1

                         
      
      
        where
      
       m.id ==
      
         id

                         
      
      
        select
      
      
         m.pict).FirstOrDefault();

            
      
      
        var
      
       stream = 
      
        new
      
      
         MemoryStream(image.ToArray());

            
      
      
        return
      
      
        new
      
       FileStreamResult(stream, 
      
        "
      
      
        image/jpg
      
      
        "
      
      
        );

        }
      
    

视图

      
        <
      
      
        img 
      
      
        src
      
      
        ="@Url.Action("
      
      
        ShowImg","Image",new{id
      
      
        = item.id})" 
      
      
        height
      
      
        ="117px"
      
      
         width
      
      
        ="144px"
      
      
         alt
      
      
        =""
      
      
        />
      
    

 

图片的数据库存取


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论