0

I want to upload a image to my server form iPhone To upload image my code is

UIImage *image = [UIImage imageNamed:@"bg_DName.png"];
NSMutableData *imageData  = (NSData*)UIImagePNGRepresentation(image);

     NSString *string = [NSString stringWithFormat:@"http://myServer/HeritageWebServices/Service.asmx/testuploadimage"];

    [self setRequest1:[ASIFormDataRequest requestWithURL:[NSURL URLWithString:string]]];
    [request1 setPostValue:@"test" forKey:@"value1"];
    [request1 setPostValue:@"test" forKey:@"value2"];
    [request1 setPostValue:@"test" forKey:@"value3"];
    [request1 setTimeOutSeconds:20];
    [request1 setDelegate:self];
    [request1 setDidFailSelector:@selector(uploadFailed:)];
    [request1 setDidFinishSelector:@selector(uploadFinished:)];
    [request1 setPostBody:imageData];


//    NSLog(@"image %@",imageData);
   [request1 setData:imageData withFileName:@"photo.png" andContentType:@"image/png" forKey:@"photo"];
[request1 startAsynchronous];

With the above code i am not able upload image.

UIImage *image = [UIImage imageNamed:@"bg.png"];
    NSData *imageData  = UIImagePNGRepresentation(image);
    //NSLog(@"imageData %@",imageData);
    NSString *dt = [[imageData description] stringByTrimmingCharactersInSet:     
                    [NSCharacterSet characterSetWithCharactersInString:@"<>"]]; 
    dt = [dt stringByReplacingOccurrencesOfString:@" " withString:@""];
    NSString *string = [NSString stringWithFormat:@"http://myServer/HeritageWebServices/Service.asmx/testuploadimage?image=%@",dt];
   // NSLog(@"urlstring %@",string);
      request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:string]];
    //    
    [request startAsynchronous];

If i try like this i can upload small image but not large images.Here i am sending the image in the request parameter as string.

My c# code to receive image

[WebMethod]
       public byte[] testuploadimage(string image)
       {
           byte[] imageBytes;
           System.Net.HttpWebRequest httpWebRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create("http://myServer/HeritageWebServices/Service.asmx/testuploadimage");
           httpWebRequest.Method = "POST";
           httpWebRequest.ContentType = "application/octet-stream";
           httpWebRequest.ContentLength = image.Length;

           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("~/Images/");
               string s = image.Replace(" ", string.Empty);

               ErrLogMgr.LogErrorMessage(string.Format("{0}{1}", "testuploadimage() for the image :",
                                                      image), "testUploadimage Inputs",
                                                       ERRORSOURCE.CSASERVICE);
               string imgname = DateTime.UtcNow.ToString().Replace(" ", "").Replace("AM", "").Replace("PM", "").Replace("/", "").Replace("-", "").Replace(":", "") + ".png";
               //       string imgname = DateTime.UtcNow.ToString("yyyyMMddHHmm") + ".png";

               //  byte[] imageBytes = Convert.FromBase64String(image.Replace(" ","+"));
               imageBytes = HexStringToByteArray(s);
               MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length);
               // MemoryStream ms = new MemoryStream(imageBytes);
               // Convert byte[] to Image
               //  ms.Write(imageBytes, 0, imageBytes.Length);
               ErrLogMgr.LogErrorMessage(string.Format("{0}{1}", "testuploadimage() for the image :",
                                                      image), "testUploadimage Inputs",
                                                       ERRORSOURCE.CSASERVICE);

               Image image2 = Image.FromStream(ms);
               ErrLogMgr.LogErrorMessage(string.Format("{0}{1}", "testuploadimage() for the image :",
                                                      image), "testUploadimage Inputs",
                                                       ERRORSOURCE.CSASERVICE);
               // System.Drawing.Bitmap image2 =  new System.Drawing.Bitmap(ms);
               image2.Save(actFolder + imgname);


               XmlElement root1 = login.CreateElement("uploaded");
               root1.InnerText = "true";
               root.AppendChild(root1);
               XmlElement root2 = login.CreateElement("path");
               root2.InnerText = "http://myServer/HeritageWebServices/Images/" + imgname;
               root.AppendChild(root2);

              // return login;
               return imageBytes;

          // }
           //catch (Exception ex)
           //{
           //    ErrLogMgr.LogErrorMessage(string.Format("{0}{1}", "testuploadimage() for the image :",
           //                                           image), "testUploadimage Inputs",
           //                                            ERRORSOURCE.CSASERVICE);
           //    XmlDocument cd = new XmlDocument();
           //    cd.LoadXml("<Message>" + ex + "</Message>");
           //   // return cd;
           //    return imageBytes;
           //}


       }


       private byte[] HexStringToByteArray(string hexString) 
       {
           int bytesCount = (hexString.Length) / 2;
           byte[] bytes = new byte[bytesCount];

           for (int x = 0; x < bytesCount; ++x)
           {
               bytes[x] = Convert.ToByte(hexString.Substring(x * 2, 2), 16);
           }

           return bytes;
       } 
   }

For the large image exception which i am getting on server is

System.Runtime.InteropServices.ExternalException (0x80004005): A generic error occurred in GDI+. at System.Drawing.Image.Save(String filename, ImageCodecInfo encoder, EncoderParameters encoderParams) at System.Drawing.Image.Save(String filename, ImageFormat format) at System.Drawing.Image.Save(String filename) at Heritage.Service.testuploadimage(String image) –</p>

Can anybody help me.. What am i doing wrong Is the problem with my iPhone code of with my c# code. Thanx!!!

4

1 回答 1

0

0x80004005 转换为拒绝访问。因此,它看起来像是服务器端配置。

查看您尝试将图像另存为的路径的权限。还要确保 appPool 身份或最终用户具有写入权限(取决于您的 asp.net 安全配置/委托设置)。

于 2012-07-10T00:31:39.713 回答