【问题标题】:how to upload the image from iphone and using .NET webservices如何从 iphone 上传图像并使用 .NET 网络服务
【发布时间】:2011-06-06 16:17:00
【问题描述】:

我想使用 .NET 网络服务上传图片,这将从 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

【问题讨论】:

  • 您能否添加您收到的错误消息?堆栈跟踪也是如此?
  • @Jonathan 我已更新错误。

标签: c# iphone .net web-services base64


【解决方案1】:
[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;
    }

}

我得到了答案...

谢谢大家....

【讨论】:

    【解决方案2】:

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

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

    查看您发送的链接,iPhone 看起来像是在发送带有空格的十六进制字符。

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

    • Here 是带有代码的链接 将十六进制转换为字节[]。
    • Here 是关于 Base64 的链接 编码是。
    • Here 是一个链接描述 十六进制,这是什么 iPhone 正在向您发送。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-04-23
      • 2014-09-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-23
      相关资源
      最近更新 更多