2

I'm writing a Ruby/Rhomobile application that takes an image, encodes it in base64 and sends to the server(that is running C# on ASP.NET), as part of a bigger XML.

However, once I try to decode the base64 and save it to the hard disk, the resulting file does not work as an image.

Here's the relevant ruby code:

image_element = REXML::Element.new("image")
image_element.text = Base64.encode64(open(Rho::RhoApplication::get_blob_path(self.image_uri)) { |io| io.read })
form_element.add_element(image_element)

And here is my C# code:

var doc = new XmlDocument();
doc.LoadXml(Server.UrlDecode(Request.Form[0]));
var imageBase64 = doc.SelectNodes("//image")[0];
var imageBytes = imageBase64.InnerText;
using(var imgWriter = new FileStream(@"c:\img.jpg",FileMode.Create))
{
    imgWriter.Write(imageBytes,0,imageBytes.Length);
}
5
  • is the client side image file definitely a JPEG? Commented Dec 28, 2011 at 14:46
  • @AdamRalph yes, definitely a JPEG. Commented Dec 28, 2011 at 15:32
  • is the size of the resulting server side file identical to that of the original client side file? Commented Dec 28, 2011 at 17:17
  • 1
    If the ruby code is running on Windows, then try opening the file in binary mode (should work with any version of ruby) or use IO.binread (ruby 1.9.1 or later). Commented Dec 28, 2011 at 23:17
  • @joast - openning in binary have solved the problem. Thanks alot! Please convert your comment into answer so that I can accept it. Commented Dec 29, 2011 at 7:19

1 Answer 1

1

I would investigate your call to Server.UrlDecode. It seems like that could corrupt your data.

It seems like the "+" sign is of specific concern, per this SO question. Server.UrlDecode uses HttpServerUtility.UrlDecode, and here's the documentation for it.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks! That actually helped to make some progress, but unfortunately was not enough. I've removed the urlencoding, and now the bytes arrive the same way they were sent. However, the resulting image is not the same image I started with.
Also, as it seems the base64 encoding/decoding yield the same bytes, so it might be that the bytes sent were wrong in the first place.
This part I'm not sure about, but your problems could be arising because you're using IO.open instead of File.open.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.