asp.netvb.netfile-uploadstreamhttppostedfilebase

How to convert Stream to Byte()


I've been struggling with this for days. I'm trying to create functionality for someone to simply upload an image file from their local machine to an FTP Server using the FileUpload Control. The problem is, the FileUpload control cannot explicitly retrieve the path of the image to be uploaded from the clients machine, and hence I cannot get the source path of the image dynamically if it were to be uploaded from any pc on the net. The only way to 'SORT OF' get the path, or rather the stream is using FileUpload.PostedFile.inputStream. The problem with this however is converting the image to a byte array. The functionality I've searched for thus far to do so have all uploaded the file to the server with 0 bytes. If i use StreamReader(FileUpload.PostedFile.InputStream) and get bytes by encoding with UTF8 the uploaded image has bytes but greater than the original file and the image is corrupted.

The Following is the Code im using to upload

Public Sub Upload()
    'FTP Server URL.
    Dim ftp As String = "ftp://winhost1.axxesslocal.co.za"

    'FTP Folder name. Leave blank if you want to upload to root folder.
    Dim ftpFolder As String = "/httpdocs/images/"

    Dim fileBytes As Byte() = Nothing

    'Read the FileName and convert it to Byte array.
    Dim fileName As String = Path.GetFileName(ImageUpload.FileName)
    Using fileStream As New StreamReader(ImageUpload.PostedFile.InputStream)

        fileBytes = Encoding.UTF8.GetBytes(fileStream.ReadToEnd())
        fileStream.Close()
    End Using

    'Create FTP Request.
    Dim request As FtpWebRequest = DirectCast(WebRequest.Create(ftp & ftpFolder & fileName), FtpWebRequest)
        request.Method = WebRequestMethods.Ftp.UploadFile

        'Enter FTP Server credentials.
        request.Credentials = New NetworkCredential("******", "******")
        request.ContentLength = fileBytes.Length
        request.UsePassive = True
        request.UseBinary = True
        request.ServicePoint.ConnectionLimit = fileBytes.Length
        request.EnableSsl = False

    Using requestStream As Stream = request.GetRequestStream()
            requestStream.Write(fileBytes, 0, fileBytes.Length)
            requestStream.Close()
        End Using

        Dim response As FtpWebResponse = DirectCast(request.GetResponse(), FtpWebResponse)


        response.Close()

End Sub

i know the issue is here fileBytes = Encoding.UTF8.GetBytes(fileStream.ReadToEnd())

But I dont know how else to convert ImageUpload.PostedFile.InputStream() to bytes that will give me an undistorted image.


Solution

  • You don't need to do any UTF encoding or decoding, nor do you need a StreamReader. Just grab the bytes.

    fileStream = ImageUpload.PostedFile.InputStream
    Dim fileBytes(0 to fileStream.Length - 1) as Byte
    fileStream.Read(fileBytes, 0, fileBytes.Length)
    fileStream.Close()
    

    Or if you prefer to receive the buffer as a return value, you can use a BinaryReader:

    Using binaryReader As New BinaryReader(ImageUpload.PostedFile.InputStream)
        fileBytes = binaryReader.ReadBytes(binaryReader.BaseStream.Length)
        binaryReader.Close()
    End Using