将传进来的文件转换成字符串

        /// <summary>
        /// 将传进来的文件转换成字符串
        /// </summary>
        /// <param name="FilePath">待处理的文件路径(本地或服务器)</param>
        /// <returns></returns>
        private string FileToBinary(string FilePath)
        {
            FileStream fs = new FileStream(FilePath, FileMode.Open, FileAccess.Read);
            string strData = "";
            try
            {
                //利用新传来的路径实例化一个FileStream对像
                int fileLength = Convert.ToInt32(fs.Length);


                //得到对像大小
                byte[] fileByteArray = new byte[fileLength];


                //声明一个byte数组
                BinaryReader br = new BinaryReader(fs);


                //声明一个读取二进流的BinaryReader对像
                for (int i = 0; i < fileLength; i++)
                {
                    //循环数组
                    br.Read(fileByteArray, 0, fileLength);
                }
                strData = Convert.ToBase64String(fileByteArray).ToString().Replace("+", "%2B");
                fs.Close();
            }
            catch (Exception ef)
            {
                Log.Error(ef.Message);               
            }


            return strData;
        }

猜你喜欢

转载自blog.csdn.net/ahao214/article/details/77962250
今日推荐