Base64字符串转二进制流保存文件到服务器

		 /// 
        /// 图片上传 Base64解码
        /// 
        /// Base64数据
        /// 返回一个相对路径

        public static Dictionary<string, string> decodeBase64ToImage(string dataURL)
        {
            if (string.IsNullOrEmpty(dataURL))
                return new Dictionary<string, string>();
            var Current = HttpContext.Current;

            string path = "/UserFile/Files/Attach/" + DateTime.Now.ToString("yyyyMMdd");
            string savepath = Current.Server.MapPath("~/" + path);

            if (!Directory.Exists(savepath))
                Directory.CreateDirectory(savepath);
            string filepath = "";
            string imgName = "face_" + ALUtils.GetGUID() + ".JPEG"; ;//声明一个string类型的相对路径

            try//会有异常抛出,try,catch一下
            {
                filepath = path + "/" + imgName;//所要保存的相对路径及名字
                string savepathfile = savepath + "/" + imgName;//所要保存的相对路径及名字
                FileStream fs = new FileStream(savepathfile, FileMode.Create, FileAccess.Write);
                BinaryWriter bw = new BinaryWriter(fs);
                byte[] arr = Convert.FromBase64String(dataURL);
                bw.Write(arr);
                fs.Close();
                bw.Close();
            }
            catch (Exception ex)
            {
                AddLgoToTXT(ex.Message + "  " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            }
            var dic = new Dictionary<string, string>();
            dic.Add(imgName, filepath);
            return dic;//返回相对路径
        }

你可能感兴趣的:(C#)