简体   繁体   中英

Ruby base64 to C# base64

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);
}

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM