2012-07-09 75 views
-1

C#服務器我想將圖片上傳到我的服務器形式iPhone 要上傳圖片我的代碼是上傳圖像從iPhone

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]; 

與上面的代碼我不能夠上傳圖片。

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]; 

如果我嘗試這樣我可以上傳小圖像,但不是大圖像。在這裏我發送請求參數中的圖像作爲字符串。

我的C#代碼接收圖像

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

對此我從服務器上的大圖像的例外是

System.Runtime.InteropServices.ExternalException(0X80004005):在GDI發生一般性錯誤+ 。 System.Drawing.Image.Save(字符串文件名,ImageCodecInfo編碼器,EncoderParameters encoderParams)在System.Drawing.Image.Save(字符串文件名,ImageFormat格式)在System.Drawing.Image.Save(字符串文件名)在Heritage.Service。 testuploadimage(字符串圖像) -

任何人都可以幫助我..我做錯了什麼 是我的iPhone代碼與我的C#代碼的問題。 Thanx!

+0

您需要添加您在服務器上遇到的異常或客戶端上的錯誤。另外,服務器代碼是否會受到影響?跟蹤,日誌,附加,東西... – bryanmac 2012-07-09 13:16:08

+0

這是我得到的異常,如果我上傳大圖像文件System.Runtime.InteropServices.ExternalException(0x80004005):在GDI +發生一般性錯誤。 System.Drawing.Image.Save(字符串文件名,ImageCodecInfo編碼器,EncoderParameters encoderParams)在System.Drawing.Image.Save(字符串文件名,ImageFormat格式)在System.Drawing.Image.Save(字符串文件名)在Heritage.Service。 testuploadimage(String image) – 2012-07-09 13:21:50

+0

@SurenderRathore - 用例外更新你的問題。聽起來問題是服務器端代碼。 – 2012-07-09 13:30:10

回答

0

0x80004005轉換爲拒絕訪問。所以,它看起來像是服務器端配置。

查看您嘗試將圖像保存爲的路徑的權限。同時確保appPool身份或最終用戶擁有寫入權限(取決於您的asp.net安全配置/委派設置)。

+0

Thanx的答覆...這種異常只發生如果我發送大圖像..我該怎麼做才能解決這個問題.. – 2012-07-10 07:38:10