locked
Reading a file into a byte array RRS feed

  • Question

  • User1635080783 posted

    I need to read a base64 string into a byte array, with the intention of converting it from base64 to binary and writing it back out as a jpeg. (Because that's what it is-- a base-64 encoded jpeg image)

    The following is what I have so far:

                 Dim fpath As String = Server.MapPath("myfile.txt")
     
                Dim fs As System.IO.FileStream
                fs = File.Open(fpath, FileMode.Open, FileAccess.Read, FileShare.None)
                Dim filelen As Long = fs.Length
                Dim buffer As Byte()

                Try
                    fs.Read(buffer, 0, filelen)
                Catch ex As Exception
                    Trace.Write("ERROR", " Error: " & ex.Message)
                End Try

                fs.Read(buffer, 0, filelen)

     

    When I compile it I get a  warning:  Variable 'buffer' is used before it has been assigned a value. A null reference exception could result at runtime.

    And this is the error message I get when I run it:

    Buffer cannot be null.
    Parameter name: array

     So how do I initialize "buffer"?  Or is there a better way to do this?

    Thanks very much.

     

     

     

    Wednesday, December 17, 2008 10:01 AM

Answers

  • User-1657381277 posted

    You are missing this. Dim buff(fs.Length) As Byte. Also keep in mind if the file size is big, (500 MB or greater you may want to read it in in chunks.  Also put a try catch around it in case things hang.

     

                Dim fpath As String = Server.MapPath("myfile.txt")

                If fi.Exists AndAlso fileName.Length > 0 Then
                      Dim
    fs As System.IO.FileStream = System.IO.File.Open(fpath, IO.FileMode.Open, IO.FileAccess.Read)
                      Dim buff(fs.Length) As Byte
                      If
    fs.Length > 0 Then
                            fs.Read(buff, 0, fs.Length - 1)
                            fs.Close()
                            buff = Nothing
                            fs = Nothing
                      Else
                            'Empty File
                      End If

                End If

      
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, December 17, 2008 11:22 AM

All replies

  • User-1657381277 posted

    You are missing this. Dim buff(fs.Length) As Byte. Also keep in mind if the file size is big, (500 MB or greater you may want to read it in in chunks.  Also put a try catch around it in case things hang.

     

                Dim fpath As String = Server.MapPath("myfile.txt")

                If fi.Exists AndAlso fileName.Length > 0 Then
                      Dim
    fs As System.IO.FileStream = System.IO.File.Open(fpath, IO.FileMode.Open, IO.FileAccess.Read)
                      Dim buff(fs.Length) As Byte
                      If
    fs.Length > 0 Then
                            fs.Read(buff, 0, fs.Length - 1)
                            fs.Close()
                            buff = Nothing
                            fs = Nothing
                      Else
                            'Empty File
                      End If

                End If

      
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, December 17, 2008 11:22 AM
  • User1635080783 posted

     Thanks very much for your help!!

    Wednesday, December 17, 2008 1:41 PM
  • User-266950512 posted

    Dim buff(fs.Length) As Byte

    should be

    Dim buff(fs.Length-1) As Byte

    Monday, April 15, 2013 7:14 PM