title: Using asmx file for uploading image from iphone
body: <p>Im struggling here for a lot,
  Im trying to upload an image from iphone to (iis)server folder using webservice asmx(VB.net)</p>
  
  <p>Ive searched a lot and finally used the following code</p>
  
  <pre><code>- (IBAction)btnPostImages_Clicked:(id)sender  {
  
  NSMutableURLRequest *request;
  
  request = [[[NSMutableURLRequest alloc] init] autorelease];
  
  [request setHTTPMethod:@"POST"];
  
  [request setURL:[NSURL URLWithString:@"http://192.168.0.2/digita/digitacampus.asmx/SaveImage"]];
  
  NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
  
  NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
  
  [request addValue:contentType forHTTPHeaderField: @"Content-Type"];
  
  NSMutableData *body = [NSMutableData data];
  
  [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
  
  [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"%@\"\r\n",@"recFile",@"image.jpg"]dataUsingEncoding:NSUTF8StringEncoding]];
  
  
  [body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
  
  [body appendData:[NSData dataWithData:dt]];
  
  [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
  
  [request setHTTPBody:body];
  
  NSURLConnection *conn=[[NSURLConnection alloc] initWithRequest:request delegate:self];
  
  }
  </code></pre>
  
  <p>dt is NSData of image in jpeg representation </p>
  
  <p>and the webmethod as follows</p>
  
  <pre><code>&lt;WebMethod()&gt; _
  Public Function SaveImage(ByVal recFile As String) As String
      Dim file As HttpPostedFile = HttpContext.Current.Request.Files("recFile")
      Dim targetFilePath As String = HttpContext.Current.Server.MapPath("") + file.FileName
      file.SaveAs(targetFilePath)
      Return file.FileName.ToString()
  End Function
  </code></pre>
  
  <p>Nothing happened</p>
  
  <p>Is the above webmethod correct</p>
  
  <p>I have also tried simply as below</p>
  
  <pre><code>    - (IBAction)btnPostImages_Clicked:(id)sender  {
  
  NSURL *url = [NSURL URLWithString:@"http://192.168.0.2/digita/digitacampus.asmx/SaveImage"];
  
  NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
  
  [req addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
  
  [req addValue:@"image/jpeg" forHTTPHeaderField:@"Content-Type"];
  
  [req setHTTPMethod:@"POST"];
  
  [req setHTTPBody:dt];
  
  NSURL *conn = [[NSURLConnection alloc] initWithRequest:req delegate:self];
  }
  </code></pre>
  
  <p>I dont know what to do else </p>
  
  <p>whats the mistake that I did</p>
  
  <p>How can I identify the image was successfully uploaded or not</p>