- A+
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | public class Base64Convert { /// <summary> /// 文件转换成Base64字符串 /// </summary> /// <param name="fileName">文件绝对路径</param> /// <returns></returns> public static String FileToBase64(string fileName) { string strRet = null; try { FileStream fs = new FileStream(fileName, FileMode.Open); byte[] bt = new byte[fs.Length]; fs.Read(bt, 0, bt.Length); strRet = Convert.ToBase64String(bt); fs.Close(); } catch (Exception ex) { throw ex; } return strRet; } /// <summary> /// Base64字符串转换成文件 /// </summary> /// <param name="strInput">base64字符串</param> /// <param name="fileName">保存文件的绝对路径</param> /// <returns></returns> public static bool Base64ToFileAndSave(string strInput, string fileName) { bool bTrue = false; try { byte[] buffer = Convert.FromBase64String(strInput); FileStream fs = new FileStream(fileName, FileMode.CreateNew); fs.Write(buffer, 0, buffer.Length); fs.Close(); bTrue = true; } catch (Exception ex) { throw ex; } return bTrue; } |
- 我的微信
- 这是我的微信扫一扫
- 我的微信公众号
- 我的微信公众号扫一扫