C#图片存储数据库

前端js

function uploadImg1() {
        var f = document.getElementById('uploadFile1').files[0]
        var src = window.URL.createObjectURL(f);
        document.getElementById('uploadPreview1').src = src;


        learun.loading(true, '正在保存...');
        $.ajaxFileUpload({
            url: top.$.rootUrl + "/LR_PortalSite/HomeConfig/UploadFile?type=4",
            secureuri: false,
            fileElementId: 'uploadFile1',
            dataType: 'json',
            success: function (data) {
                learun.loading(false);
            }
        });
    };

后台

/// <summary>
        /// 保存图片和存储数据
        /// </summary>
        /// <param name="type">类型</param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult UploadFile(string type)
        {
            HttpFileCollection files = System.Web.HttpContext.Current.Request.Files;
            //没有文件上传,直接返回
            if (files[0].ContentLength == 0 || string.IsNullOrEmpty(files[0].FileName))
            {
                return HttpNotFound();
            }
            else
            {
                string FileEextension = Path.GetExtension(files[0].FileName);
                byte[] bytes = new byte[files[0].InputStream.Length];
                files[0].InputStream.Read(bytes, 0, bytes.Length);

                string strBase64 = Convert.ToBase64String(bytes);
                homeConfigIBLL.SaveImg(strBase64, files[0].FileName, FileEextension, type);
            }

            return Success("保存成功。");
        }

获取后台

 public void GetImg(string keyValue)
        {
            ArticleEntity entity = GetEntity(keyValue);
            string img = "";
            if (entity != null && !string.IsNullOrEmpty(entity.F_Img))
            {
                ImgEntity imgEntity = imgIBLL.GetEntity(entity.F_Img);

                if (imgEntity != null && !string.IsNullOrEmpty(imgEntity.F_Content))
                {
                    FileDownHelper.DownLoadBase64(imgEntity.F_Content, imgEntity.F_Name);
                    return;
                }
            }
            else
            {
                img = "/Content/images/plhome/banner_df.jpg";
            }

            if (string.IsNullOrEmpty(img))
            {
                img = "/Content/images/plhome/banner_df.jpg";
            }
            FileDownHelper.DownLoad(img);
        }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值