如何从iphone上传图像并使用.NET Web服务

本文关键字:NET Web 服务 图像 iphone | 更新日期: 2023-09-27 17:58:26

我想使用.NET Web服务上传图像,这将从iphone图像中调用。

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

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

在哪个数据类型中,我必须转换这些数据,然后将其保存为图像格式。

如果你有其他方法,请告诉我。

我已尝试将此数据转换为字节[],但它给了我错误。这是我的密码。为了我所尝试的。请帮帮我。

[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

如何从iphone上传图像并使用.NET Web服务

[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看起来像是在发送带空格的十六进制字符。

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

  • 这是一个带有代码的链接将十六进制转换为字节[]
  • 以下是Base64的链接编码是
  • 以下是描述十六进制,这是iPhone正在发送您