Bhavik Goyal
Bhavik Goyal

Reputation: 2796

how to upload the image from iphone and using .NET webservices

I want to upload the image using the .NET webservices and that will be called from iphone image.

the format of text that iphone is sending to me is like this.

http://d8768157.u118.c6.ixwebhosting.com/iphoneimg/image.txt

In which datatype i must convert this data and then save that in image format.

If you have any other method then please tell me.

I have tried converting this data in the byte[] but it is giving me error. here is my code. For what i have tried. please help me out.

[WebMethod]
    public XmlDocument testuploadimage(string image)
    {
        XmlDocument login = new XmlDocument();
        XmlDeclaration dec = login.CreateXmlDeclaration("1.0", null, null);
        login.AppendChild(dec);
        XmlElement root = login.CreateElement("CreateUser");
        login.AppendChild(root);
        try
        {

            string actFolder = Server.MapPath("~/iphoneimg/");
            string imgname = DateTime.UtcNow.ToString().Replace(" ", "").Replace("AM", "").Replace("PM", "").Replace("/", "").Replace("-", "").Replace(":", "") + ".png";

            Bitmap map;

            using (MemoryStream stream = new MemoryStream(Convert.FromBase64String(image)))
            using (FileStream fs = File.Create(actFolder + imgname))
            {
                map = (Bitmap)Image.FromStream(stream);
                map.Save(fs, System.Drawing.Imaging.ImageFormat.Gif);
            }

            XmlElement root1 = login.CreateElement("uploaded");
            root1.InnerText = "true";
            root.AppendChild(root1);
            XmlElement root2 = login.CreateElement("path");
            root2.InnerText = "http://d8768157.u118.c6.ixwebhosting.com/iphoneimg/" + imgname;
            root.AppendChild(root2);

            return login;
        }
        catch (Exception ex)
        {
            throw ex;
        }

    }

this is the error i m getting

Invalid character in a Base-64 string.

Thanks

BHAVIK GOYAL

Upvotes: 0

Views: 2694

Answers (2)

Bhavik Goyal
Bhavik Goyal

Reputation: 2796

[WebMethod]
public XmlDocument testuploadimage(string image)
{
    XmlDocument login = new XmlDocument();
    XmlDeclaration dec = login.CreateXmlDeclaration("1.0", null, null);
    login.AppendChild(dec);
    XmlElement root = login.CreateElement("CreateUser");
    login.AppendChild(root);
    try
    {

        string actFolder = Server.MapPath("~/iphoneimg/");
        string imgname = DateTime.UtcNow.ToString().Replace(" ", "").Replace("AM", "").Replace("PM", "").Replace("/", "").Replace("-", "").Replace(":", "") + ".Png";

        byte[] imageBytes = Convert.FromBase64String(image.Replace(" ","+"));
        MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length);

        // Convert byte[] to Image
        ms.Write(imageBytes, 0, imageBytes.Length);
        System.Drawing.Image image2 = System.Drawing.Image.FromStream(ms, true);
        image2.Save(actFolder + imgname);


        XmlElement root1 = login.CreateElement("uploaded");
        root1.InnerText = "true";
        root.AppendChild(root1);
        XmlElement root2 = login.CreateElement("path");
        root2.InnerText = "http://d8768157.u118.c6.ixwebhosting.com/iphoneimg/" + imgname;
        root.AppendChild(root2);

        return login;
    }
    catch (Exception ex)
    {
        throw ex;
    }

}

I got the answers...

thanks to all....

Upvotes: 1

jonathanpeppers
jonathanpeppers

Reputation: 26495

The error you're getting is a fundamental problem with your situation.

You are passing a non-Base64 string to Convert.FromBase64String().

Looking at the link you sent, the iPhone looks like it's sending hex characters with spaces.

You options are to either have the iPhone send you Base64, or remove the spaces and convert what the iPhone is sending now.

  • Here is a link with code converting Hex to byte[].
  • Here is a link on what Base64 encoding is.
  • Here is a link describing hexadecimal, which is what the iPhone is sending you.

Upvotes: 0

Related Questions