文件转换为Base64输出功能 [英] convert file to base64 function output

查看:202
本文介绍了文件转换为Base64输出功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 公共功能ConvertFileToBase64(BYVAL文件名作为字符串)作为字符串    昏暗返回值作为字符串=    如果My.Computer.FileSystem.FileExists(文件名)然后
        使用BinaryFile作为的FileStream =新的FileStream(文件名,FileMode.Open)
            昏暗为BinRead作为BinaryReader =新BinaryReader(BinaryFile)
            昏暗BinBytes为字节()= BinRead.ReadBytes(CINT(BinaryFile.Length))
            返回值= Convert.ToBase64String(BinBytes)
            BinaryFile.Close()
        使用完
    万一
    返回返回值
结束功能

这些问题我要问:


  1. 我要的输出,我想转换一个文本文件C:\\用户\\用户\\桌面\\老鼠\\测试\\ test.txt的
    为Base64

  2. 我无法得到任何输出,当我转换的test.txt为base64

  3. 我使用的文件名=textbox1.text加我的test.txt


解决方案

 公共功能ConvertFileToBase64(BYVAL文件名作为字符串)作为字符串
        返回Convert.ToBase64String(System.IO.File.ReadAllBytes(文件名))
    结束功能

Public Function ConvertFileToBase64(ByVal fileName As String) As String

    Dim ReturnValue As String = ""

    If My.Computer.FileSystem.FileExists(fileName) Then
        Using BinaryFile As FileStream = New FileStream(fileName, FileMode.Open)
            Dim BinRead As BinaryReader = New BinaryReader(BinaryFile)
            Dim BinBytes As Byte() = BinRead.ReadBytes(CInt(BinaryFile.Length))
            ReturnValue = Convert.ToBase64String(BinBytes)
            BinaryFile.Close()
        End Using
    End If
    Return ReturnValue
End Function

The questions I want to ask:

  1. I want the output that is I want to convert a text file "C:\Users\user\Desktop\rats\test\test.txt" to base64
  2. I was unable to get any output when i converted "test.txt" to base64
  3. I use filename="textbox1.text" to add my "test.txt"

解决方案

   Public Function ConvertFileToBase64(ByVal fileName As String) As String
        Return Convert.ToBase64String(System.IO.File.ReadAllBytes(fileName))
    End Function

这篇关于文件转换为Base64输出功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆