title: iPhone: NSData and Byte conversation issue?
body: <p>I want to send iPhone image to server database. I wrote following code. I am getting data mismatch exception and not able to resolve it. Could anyone please help me out.</p>
  
  <p>Code on iPhone side:</p>
  
  <pre><code>- (void) uploadUserInfo:(NSString*)userId profilePic:(UIImage*)profilePic {
  
      __block ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL: [NSURL URLWithString:[NSString stringWithFormat:@"%@/%d/uploadUserInfo?cultureDetect=false", dataUrl, currentAppID]]];
  
      [request setPostValue:userId forKey: @"userId"];
      // Upload an image
      [request addPostValue:@"pic.jpg" forKey:@"fileName"];
      NSData* imageData = UIImageJPEGRepresentation(profilePic, 1);
      [request setData:imageData withFileName:@"pic.jpg" andContentType:@"image/jpeg" forKey:@"profilePic"];
  
      [request setDelegate:self];
      [request setDidFinishSelector:@selector(uploadRequestFinished:)];
      [request setDidFailSelector:@selector(uploadRequestFailed:)];
  
      [queue addOperation: request];
      [queue go];
  </code></pre>
  
  <p>}</p>
  
  <p>Code on ASP.NET MVC website Controller:</p>
  
  <pre><code>    public void uploadUserInfo(string userId, string fileName, byte[] profilePic)
      {
          int ID;
          if (int.TryParse(appId, out ID) &amp;&amp; fileName != null &amp;&amp; profilePic != null)
          {
              Image model = new Image();
              System.Drawing.Image image = System.Drawing.Image.FromStream(new System.IO.MemoryStream(profilePic));
              System.Drawing.Point maxSize = FileUtils.GetImageMaxSize(Image.ImageTypeEnum.TableCellImage);
              System.Drawing.Image thumb = ImageUtilities.GetFixedSizeImage(image, InterpolationMode.HighQualityBicubic, null, PixelFormat.Format32bppArgb, maxSize.X, maxSize.Y, false);
              byte[] resizedContent = ImageUtilities.GetBytesFromImage(thumb, ImageFormat.Png);
              model.AmendedDateTime = DateTime.Now;
              model.ApplicationId = ID;
              model.ImageType = Image.ImageTypeEnum.TableCellImage;
              string contentType = ImageUtilities.GetContentType(resizedContent);
  
              FileInfo f = new FileInfo(fileName);
              string name = f.Name.Substring(0, f.Name.Length - f.Extension.Length);
              string extension = f.Extension.Substring(1);
  
              BinaryFile file = new BinaryFile()
              {
                  ContentType = contentType,
                  FileName = name,
                  FileExtension = extension
              };
  
              file.Data = resizedContent;
              model.BinaryFile = file;
  
              this.DataContext.Images.Table.InsertOnSubmit(model);
  
              ValidateAndSubmitChanges();
          }
          return;
      }
  </code></pre>
  
  <p>I have been getting following error before my method start executing. i.e. I guess MVC engine throwing exception directly when data type mismatched.</p>
  
  <pre><code> [FormatException]: The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or a non-white space character among the padding characters. 
     at System.Convert.FromBase64String(String s)
     at System.Web.Mvc.ByteArrayModelBinder.BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
     at System.Web.Mvc.ControllerActionInvoker.GetParameterValue(ControllerContext controllerContext, ParameterDescriptor parameterDescriptor)
     at System.Web.Mvc.ControllerActionInvoker.GetParameterValues(ControllerContext controllerContext, ActionDescriptor actionDescriptor)
     at System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName)
     at System.Web.Mvc.Controller.ExecuteCore()
     at System.Web.Mvc.MvcHandler.&lt;&gt;c__DisplayClass8.&lt;BeginProcessRequest&gt;b__4()
     at System.Web.Mvc.Async.AsyncResultWrapper.&lt;&gt;c__DisplayClass1.&lt;MakeVoidDelegate&gt;b__0()
     at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
     at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean&amp; completedSynchronously)
  </code></pre>
  
  <p>I tried converting NSData to Byte but ASIHTTPRequest doesn't allow to setData as Byte data type:</p>
  
  <pre><code>    NSUInteger len = [imageData length];
      Byte *byteData = (Byte*)malloc(len);
      memcpy(byteData, [imageData bytes], len);
      [request setData:byteData withFileName:@"pic.jpg" andContentType:@"image/jpeg" forKey:@"profilePic"];
  </code></pre>
  
  <p>Conversion to Base64 gives the same exception!</p>
  
  <p>using ASIHTTPRequest inbuilt static method:</p>
  
  <pre><code>  NSData* imageData = UIImageJPEGRepresentation(profilePic, 1);
    [request setData:[ASIHTTPRequest base64forData:imageData] withFileName:@"pic.jpg" andContentType:@"image/jpeg" forKey:@"profilePic"];
  </code></pre>
  
  <p>using method given at <a href="https://stackoverflow.com/questions/392464/any-base64-library-on-iphone-sdk">this</a> thread:</p>
  
  <pre><code>    NSData* imageData = UIImageJPEGRepresentation(profilePic, 1);
      [request setData:[NSString base64StringFromData:imageData length:[imageData length]]  withFileName:@"pic.jpg" andContentType:@"image/jpeg" forKey:@"profilePic"];
  </code></pre>