docドキュメントのBase 64符号化および復号化
6493 ワード
1 string filepath = String.Format("{0}\\{1}\\"+docTitle, mapPath, fileName);//
2 string newFilePath = String.Format("{0}\\{1}\\NewFile.doc", mapPath, fileName);//
3 if (File.Exists(filepath))
4 {
5 String srcBt = EncodeFileToString(filepath);//
6 DecodeBaseCodeToFile(srcBt, newFilePath);//
}
///
/// Base64
///
/// ,
///
public String EncodeFileToString(String srcFile)
{
Byte[] srcBt;
FileStream srcFS = new FileStream(srcFile, FileMode.Open);
srcBt = new byte[srcFS.Length];
srcFS.Read(srcBt, 0, srcBt.Length);
srcFS.Close();
String destStr = EncodeToByte(srcBt);
return destStr;
}
public String EncodeToByte(Byte[] bt)
{
return System.Convert.ToBase64String(bt);
}
///
/// Base64
///
/// ,
/// , , ,
///
public void DecodeBaseCodeToFile(String srcBase64Code, String desFile)
{
//
Byte[] myBt = DecodeToByte(srcBase64Code);
if (File.Exists(desFile))
{
File.Delete(desFile);
}
//
using (FileStream fs = new FileStream(desFile, FileMode.CreateNew))
{
fs.Write(myBt, 0, myBt.Length);
}
}
public Byte[] DecodeToByte(String content)
{
Byte[] bt = System.Convert.FromBase64String(content);
return bt;
}
///
///
///
/// ,
///
public Byte[] EncodeFileToString1(String srcFile)
{
FileStream srcFS = new FileStream(srcFile, FileMode.Open, FileAccess.Read);
Stream sm = srcFS;
byte[] bytes = new byte[sm.Length];
sm.Read(bytes, 0, Convert.ToInt32(sm.Length));
sm.Flush();
sm.Close();
return bytes;
}
転載先:https://www.cnblogs.com/jinaczg/p/3829254.html