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);
}
Svarog
  • 2,188
  • 15
  • 21

1 Answers1

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.

Community
  • 1
  • 1
raynjamin
  • 788
  • 5
  • 23
  • 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. – Svarog Dec 28 '11 at 15:30
  • 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. – Svarog Dec 28 '11 at 15:33
  • This part I'm not sure about, but your problems could be arising because you're using IO.open instead of File.open. – raynjamin Dec 28 '11 at 16:02