如何从iPhone上传的图片,并使用.NET Web服务上传、图片、iPhone、NET

2023-09-04 06:20:57 作者:等了几晚

我想使用.NET Web服务来上传图片,这将来自iPhone的图像调用。

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

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

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

技巧 教你用iPhone下载任何网页上的视频

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

我曾尝试将这些数据中的字节[],但它给我的错误。这里是我的code。为了什么我都试过了。请帮我。

  [WebMethod的]
    公众的XmlDocument testuploadimage(串图象)
    {
        XmlDocument的登录=新的XmlDocument();
        XmlDeclaration DEC = login.CreateXmlDeclaration(1.0,NULL,NULL);
        login.AppendChild(DEC);
        的XmlElement根= login.CreateElement(CREATEUSER);
        login.AppendChild(根);
        尝试
        {

            字符串actFolder =使用Server.Mappath(〜/ iphoneimg /);
            串imgname = DateTime.UtcNow.ToString()。替换(,).Replace(AM,).Replace(PM,).Replace(/,)。取代( - ,).Replace(:,)+的.png;

            位图的地图;

            使用(MemoryStream的流=新的MemoryStream(Convert.FromBase64String(图片)))
            使用(的FileStream FS = File.Create(actFolder + imgname))
            {
                地图=(位图)Image.FromStream(流);
                map.Save(FS,System.Drawing.Imaging.ImageFormat.Gif);
            }

            的XmlElement目录root1 = login.CreateElement(上传);
            root1.InnerText =真;
            root.AppendChild(root1下);
            的XmlElement root2 = login.CreateElement(路径);
            root2.InnerText =htt​​p://d8768157.u118.c6.ixwebhosting.com/iphoneimg/+ imgname;
            root.AppendChild(root2);

            返回的登录;
        }
        赶上(例外前)
        {
            抛出前;
        }

    }
 

这是错误I M获得

无效字符在一个base-64字符串。

感谢

BHAVIK GOYAL

解决方案

  [WebMethod的]
公众的XmlDocument testuploadimage(串图象)
{
    XmlDocument的登录=新的XmlDocument();
    XmlDeclaration DEC = login.CreateXmlDeclaration(1.0,NULL,NULL);
    login.AppendChild(DEC);
    的XmlElement根= login.CreateElement(CREATEUSER);
    login.AppendChild(根);
    尝试
    {

        字符串actFolder =使用Server.Mappath(〜/ iphoneimg /);
        串imgname = DateTime.UtcNow.ToString()。替换(,).Replace(AM,).Replace(PM,).Replace(/,)。取代( - ,).Replace(:,)+.PNG;

        字节[] imageBytes = Convert.FromBase64String(image.Replace(,+));
        MemoryStream的毫秒=新的MemoryStream(imageBytes,0,imageBytes.Length);

        //转换字节[]到图像
        ms.Write(imageBytes,0,imageBytes.Length);
        为System.Drawing.Image IMAGE2 = System.Drawing.Image.FromStream(MS,真正的);
        image2.Save(actFolder + imgname);


        的XmlElement目录root1 = login.CreateElement(上传);
        root1.InnerText =真;
        root.AppendChild(root1下);
        的XmlElement root2 = login.CreateElement(路径);
        root2.InnerText =htt​​p://d8768157.u118.c6.ixwebhosting.com/iphoneimg/+ imgname;
        root.AppendChild(root2);

        返回的登录;
    }
    赶上(例外前)
    {
        抛出前;
    }

}
 

我得到的答案...

感谢所有....

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

解决方案

[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....