• MVC保存二进制到数据库,并显示文件的方法(图片显示)


    通过两篇文章解决了数据库存储图片并显示到View的方法:

    http://blog.sina.com.cn/s/blog_6da396a50101639u.html
    http://www.cnblogs.com/artech/archive/2012/08/14/action-result-02.html

    其中,第一篇虽然有些混乱,但是对我有很大启发,而的第二篇写的就很笼统却比较了然,大体看一下就解决了令我不解的显示方法。

    1.当然第一步是Img,我们在View里,给Img的文件来源,加上Action调用:

      <img src=@Url.Action("GetImg", new { id = iii.DrId }) />

    iii就是我们的对象实例了,这里,我们调用的是当前Controller的GetImg。

    2.所以我们之后需要一个Action:

    /// <summary>
    /// Action:获取图片文件
    /// </summary>
    public FileContentResult GetImg(int id)
    {
        var anModel = zService.Find(id);
        if (anModel != null)
        {
           return File(anModel.DrImg, "image/jpg", anModel.DrId.ToString() + anModel.DrName);
        }
        else
        {
            return null;
        }
    }

    这里的方法就是拷贝的我最开始提供的文章里的,并未有太多修改,注意一下“File”方法,有三个参数:

    FileContentResult File(byte[] fileContents, string contentType, string fileDownloadName)

    第一个当然就是读取出来的文件数据了,第二个是要以什么类型展示文件(MimeType),第三个就是要展示到页面的文件,是什么文件名
    这里要注意的是,参数3,如果这样自定义的名字,生成的结果好像是:『Id+Name+"~序号"』比如:361我是超人~3.jpg


    3.至于怎么存进数据库,其实在链接文章1里有讲解,我用的是MVC和EF,代码就会类似于这样:

    3.1前台,View里加Form,注意“enctype”属性,看网上的几个文章,都说要加这个。

    @using (Html.BeginForm("Add", "DoctorAdmin", FormMethod.Post, new { enctype = "multipart/form-data" }))
    {
        <fieldset>
            <legend>DoctorInfo</legend>
    
            <div class="editor-label">
                <span>图片</span>
            </div>
            <div>
                <input type="file" name="imgs" />
            </div>
                    
            <div class="editor-label">
                @Html.LabelFor(model => model.DrName)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.DrName)
            </div>
    
            <p>
                <input type="submit" value="确定" />
            </p>
        </fieldset>
    }

    3.2后台,Action和一个获取图片方法。

            [HttpPost]
            public ActionResult Add(WXG_Doctor pModel)
            {
                if (pModel == null)
                {
                    return HttpNotFound();
                }
    
                //获取上传图片
                HttpPostedFileBase file1 = Request.Files[0];
                int error1;
                pModel.DrImg = FileUpDown.GetImageByte(out error1, file1, 1024 * 1024);
    
                zService.Create(pModel);
                return RedirectToAction("Add");
            }
    
    
    
            /// <summary>
            /// 获取图片内容
            /// </summary>
            ///<param name="pError">0:正常完成 1:文件大小异常。2:扩展名不支持。</param>
            /// <param name="pUpImage">要上传的文件</param>
            /// <param name="pFileLength">文件要小于这个大小</param>
            /// <returns>图片文件的内容</returns>
            public static byte[] GetImageByte(out int pError, HttpPostedFileBase pUpImage, int pFileLength)
            {
                if (pUpImage.FileName != null)
                {
                    //要上传的文件大小判断
                    int sFileLength = pUpImage.ContentLength;
                    if (sFileLength < 1 || sFileLength > pFileLength)
                    {
                        pError = 1;
                        return null;
                    }
    
                    //获取文件名
                    string sFilename = System.IO.Path.GetFileName(pUpImage.FileName).ToLower();
                    //获取upImage文件的扩展名
                    string extendName = System.IO.Path.GetExtension(sFilename);
                    //判断是否为图片格式 
                    if (extendName != ".jpg" && extendName != ".jpeg" && extendName != ".gif" && extendName != ".bmp" && extendName != ".png")
                    {
                        pError = 2;
                        return null;
                    }
    
                    byte[] myData = new Byte[sFileLength];
                    pUpImage.InputStream.Read(myData, 0, sFileLength);
                    pError = 0;
                    return myData;
                }
                else
                {
                    pError = 3;
                    return null;
                }
            }
        }

    其中,返回的状态那个参数,我不知会不会写的很2,而且具体的逻辑也缺乏健壮,请大家不要直接拿来就用了,酌情修改下。

  • 相关阅读:
    test
    4css
    3css
    2css
    5html
    1css
    4html
    3html
    2html
    1.3 tensorflow2.0 常用函数
  • 原文地址:https://www.cnblogs.com/3Tai/p/3548463.html
Copyright © 2020-2023  润新知