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

Posted on 2006-11-20
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:MrClyfar
  • 2
LVL 13

Accepted Solution

newyuppie earned 500 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

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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Help with sorting data in Listbox using VB.NET 3 62
SqlServer no dupes 25 35 and sql server 4 36 Progress Bar - Maximum Value too large 2 12
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 (…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

809 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