public static string FileToBase64(string filePath)
{
byte[] data = null;
try
{
FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
long numBytes = new FileInfo(filePath).Length;
//logger.LogInfo("Library.Functions.FileToBase64(): Path:" + filePath + " Bytes:" + numBytes);
data = br.ReadBytes((int)numBytes);
br.Close();
fs.Close();
}
catch (BaseException ex)
{
logger.LogError("Library.Functions.FileToBase64(): Path:" + filePath + "; Exception: " + ex.ToString(), ex);
return "";
}
if (data == null)
{
return "";
}
else
{
return Convert.ToBase64String(data);
}
}
{
byte[] data = null;
try
{
FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
long numBytes = new FileInfo(filePath).Length;
//logger.LogInfo("Library.Functions.FileToBase64(): Path:" + filePath + " Bytes:" + numBytes);
data = br.ReadBytes((int)numBytes);
br.Close();
fs.Close();
}
catch (BaseException ex)
{
logger.LogError("Library.Functions.FileToBase64(): Path:" + filePath + "; Exception: " + ex.ToString(), ex);
return "";
}
if (data == null)
{
return "";
}
else
{
return Convert.ToBase64String(data);
}
}
No comments:
Post a Comment