将文件转换为base64函数输出

时间:2012-05-24 14:15:29

标签: vb.net file base64

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

我想问的问题:

  1. 我想要输出我要转换的文本文件“C:\ Users \ user \ Desktop \ rats \ test \ test.txt” 到base64
  2. 当我将“test.txt”转换为base64
  3. 时,我无法获得任何输出
  4. 我使用filename =“textbox1.text”添加我的“test.txt”

1 个答案:

答案 0 :(得分:13)

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