如何从iphone上传图像并使用.NET webservices

我想使用.NET webservices上传图像,并将从iphone图像调用。

iphone发给我的文字格式是这样的。

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

我必须在哪种数据类型中转换此数据,然后以图像格式保存。

如果您有任何其他方法,请告诉我。

我试过在byte []中转换这些数据,但它给了我错误。 这是我的代码。 对于我所尝试的。 请帮帮我。

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

这是我得到的错误

Base-64字符串中的字符无效。

谢谢

BHAVIK GOYAL

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

我得到了答案……

谢谢大家….

您遇到的错误是您的情况的根本问题。

您正在将非Base64字符串传递给Convert.FromBase64String()。

查看您发送的链接,iPhone看起来像是发送带空格的hex字符。

您可以选择让iPhone发送给您Base64,或者删除空格并转换iPhone现在发送的内容。

  • 这是一个代码将Hex转换为byte []的链接。
  • 这是一个关于Base64编码的链接。
  • 这是一个描述hex的链接,这是iPhone发送给你的内容。