vb.net 将文件转换为 base64 函数输出

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10739264/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 16:07:23  来源:igfitidea点击:

convert file to base64 function output

vb.netfilebase64

提问by user1412843

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"
  1. 我想要的输出是我想将文本文件“C:\Users\user\Desktop\rats\test\test.txt”转换为 base64
  2. 当我将“test.txt”转换为 base64 时,我无法获得任何输出
  3. 我使用 filename="textbox1.text" 添加我的“test.txt”

回答by user2086269

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