zoukankan      html  css  js  c++  java
  • byte[] Base64 Stream 之间相互转换

    图片 base64转byte[]

    		/// <summary>
            /// 保存base64图片,返回阿里云地址
            /// </summary>
            /// <param name="imgCode"></param>
            /// <returns></returns>
            private string SaveBase64Image(string imgCode)
            {
                string imgUrl = string.Empty;
                if (!string.IsNullOrEmpty(imgCode))
                {
                    Regex reg = new Regex(@"data:(image.+);base64,(.+)");
                    if (reg.IsMatch(imgCode))
                    {
                        var matchs = reg.Match(imgCode);
                        string contentType = matchs.Groups[1].Value;
                        string base64Code = matchs.Groups[2].Value;
                        string extendType = contentType.Replace("image/", "");
                        byte[] arr = Convert.FromBase64String(base64Code);//base64转byte[]
                        //var picture = _pictureService.InsertPicture(arr, extendType);
                    }
                }
                return imgUrl;
            }
    

    Stream转byte[]

     		/// <summary>
            /// StreamToBytes
            /// </summary>
            /// <param name="stream"></param>
            /// <returns></returns>
            public static byte[] StreamToBytes(Stream stream)
            {
                byte[] bytes = new byte[stream.Length];
                stream.Read(bytes, 0, bytes.Length);
                // 设置当前流的位置为流的开始
                stream.Seek(0, SeekOrigin.Begin);
                return bytes;
            }
    

    byte[]转base64

    var base64Url = Convert.ToBase64String(avatarData);//avatarData为byte[]
    

    base64转byte[]

    byte[] data = Convert.FromBase64String(signedString);
    

    file转base64

    			var requestFiles = Request.Files;
                if (requestFiles.Count > 0)
                {
                    var avatarData = CommonHelper.StreamToBytes(requestFiles[0].InputStream);//Stream转byte[]
                    var base64Url = Convert.ToBase64String(avatarData);//byte[]转base64
                }
    
  • 相关阅读:
    JMeter 关联
    JMeter MD5加密
    JMeter 时间函数
    JMeter 常用设置
    JMeter 服务器资源监控
    js制作列表滚动(有滚动条)
    js监听事件
    获取窗口大小 并自适应大小变化
    js 标签云
    js 显示数字不断增加
  • 原文地址:https://www.cnblogs.com/zhubangchao/p/7741681.html
Copyright © 2011-2022 走看看