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
Solved

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

Posted on 2006-11-20
4
334 Views
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())

      sourceStream.Close()
      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?

Cheers.
Jas.
0
Comment
Question by:MrClyfar
  • 2
4 Comments
 
LVL 13

Accepted Solution

by:
newyuppie earned 500 total points
ID: 17980990
i use like this:

Dim fileContents() As Byte = New Byte (sourceStream.Lenght) {}
sourceStream.Read(fileContents,0,sourceStream.Lenght)
sourceStream.Close


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

0
 
LVL 13

Expert Comment

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

Expert Comment

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

Author Comment

by:MrClyfar
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)
  sourceStream.Close()

  oFTPRequest.ContentLength = fileContents.Length

  Dim requestStream As Stream = oFTPRequest.GetRequestStream()

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

End Sub

Cheers.
Jas.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
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…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

828 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