UTF8.GetBytes - Not getting the whole of JPG file.

Posted on 2006-11-20
Medium Priority
Last Modified: 2010-05-18
Hi there.

I'm trying to upload a JPG file to a FTP server.  Here is a snippet:

      Dim sourceStream As New StreamReader(sImageURL) ' <-- eg C:\Test.jpg ( about 220KB in size)

      Dim fileContents() As Byte = UTF8.GetBytes(sourceStream.ReadToEnd())

      oFTPRequest.ContentLength = fileContents.Length ' <-- Length is about 123KB????

Why is UTF8.GetBytes not returning the whole of the JPG file? I've checked the file and it's not corrupt or damaged, it's 220KB no problem. So why should the GetBytes method have an issue with the file?

Should I be using another technique for getting the bytes of a JPG file?

Question by:Jason Evans
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
LVL 13

Accepted Solution

newyuppie earned 1500 total points
ID: 17980990
i use like this:

Dim fileContents() As Byte = New Byte (sourceStream.Lenght) {}

the other option is maybe try on your existing code to add sourceStream.Flush() before closing it.

LVL 13

Expert Comment

ID: 17980995
Lenght is of course Length, typo
LVL 14

Expert Comment

ID: 17981632
Try changing Encoding from UTF8 to ASCII.
LVL 10

Author Comment

by:Jason Evans
ID: 17981802
Hi there.

By using newyuppie's suggestion, I arrived at the following code, which works spot on:

Public Sub UploadImageToShop(ByVal sImageURL As String)

  Dim oFTPRequest As FtpWebRequest = CType(WebRequest.Create(HOST & REMOTE_FILE_DIR & "/test.jpg"), FtpWebRequest)

  oFTPRequest.Method = WebRequestMethods.Ftp.UploadFile

  oFTPRequest.Credentials = New NetworkCredential(USERNAME, PASSWORD)

  Dim fileStream As FileStream = New FileStream(sImageURL, FileMode.Open)
  Dim sourceStream As New BinaryReader(fileStream)

  Dim fileContents() As Byte = New Byte(sourceStream.BaseStream.Length) {}
  sourceStream.Read(fileContents, 0, sourceStream.BaseStream.Length)

  oFTPRequest.ContentLength = fileContents.Length

  Dim requestStream As Stream = oFTPRequest.GetRequestStream()

  requestStream.Write(fileContents, 0, fileContents.Length)

End Sub


Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question