iosiphoneios7asihttprequestasiformdatarequest

Upload Image with dictionary


I want to upload image to server from iphone. Using ASIHttpRequest and ASIFormDataRequest i have sucessfully uplaoded picture to server by

[request setdata:...]

But I also want to send userId to server with image. How to achieve this? Here is my code...

  ASIFormDataRequest *_request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:URLSaveImage]];
 __unsafe_unretained ASIFormDataRequest *request = _request;
 [ASIHTTPRequest setShouldThrottleBandwidthForWWAN:YES];
 [request setUploadProgressDelegate:self];
 NSData *data=UIImageJPEGRepresentation(proImage.image,1.0);
 [request setData:data
    withFileName:@"profile.jpg"
  andContentType:@"image/jpeg"
          forKey:@"file"];

Solution

  • For POST method, use setPostValue: forKey: with the setdata:. You dont need to do any additional work . Just post the variable along with image.

    [request setPostValue:VALUE forKey:KEY];
    [request setData:data withFileName:@"profile.jpg" andContentType:@"image/jpeg"
          forKey:@"file"];