Previously I talked about calling a .NET REST full web service method that returns a object, now I’m going to talk about a how to upload a image using .NET REST web service.
Here also I’m using AFNetworking. You can download it from here.
In this example I’m sending image as an NSInputStream. I’m sending the file name and the folder name, which the image should save in the server by adding as header fields to request object.
This how your web service interface method look like.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[OperationContract] | |
[WebInvoke(UriTemplate = "UploadProfileImage", Method = "POST", BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = | |
WebMessageFormat.Json)] | |
bool UploadProfileImage(Stream data); |
This is how your service method looks like
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public bool UploadProfileImage(Stream data) | |
{ | |
try | |
{ | |
string folderName = WebOperationContext.Current.IncomingRequest.Headers.Get("folderName"); | |
string imageName = WebOperationContext.Current.IncomingRequest.Headers.Get("fileName"); | |
FileUploadManger.UploadFiles(folderName.ToLower(), imageName.ToLower(), data); | |
return true; | |
} | |
catch (Exception ee) | |
{ | |
return false; | |
} | |
} |
Here I can directly get the image data from the stream. I’m getting the image name and the folder name by calling this method from the incoming request.
WebOperationContext.Current.IncomingRequest.Headers.Get("folderName");
In iOS this is how you should call it. Hear I have written my method using blocks, so after calling the web service it will notify the caller.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
+ (void)uploadProfilePicture:(UIImage*)image : (NSString*) folderName : (NSString*) fileName withBlock:(void (^)( BOOL result, NSError *error))block { | |
NSURL *url = [NSURL URLWithString:yourServiceURL]; | |
AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL:url]; | |
NSData *data = UIImageJPEGRepresentation(image, 0.2); | |
NSInputStream *stream = [[NSInputStream alloc]initWithData:data]; | |
NSMutableURLRequest *myRequest = [client requestWithMethod:@"POST" path:@"UploadProfileImage" parameters:nil]; | |
[myRequest setValue:@"application/x-www-form-urlencoded; charset=UTF8" forHTTPHeaderField:@"Content-Type"]; | |
[myRequest setValue:folderName forHTTPHeaderField:@"folderName"]; | |
[myRequest setValue:fileName forHTTPHeaderField:@"fileName"]; | |
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]initWithRequest:myRequest]; | |
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) | |
{ | |
NSString *resultStr = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]; | |
if([resultStr isEqualToString:@"true"]) | |
{ | |
block(YES,nil); | |
} | |
else | |
{ | |
block(NO,nil); | |
} | |
} | |
failure:^(AFHTTPRequestOperation *operation, NSError *error) { | |
block(NO,nil); | |
} | |
]; | |
operation.inputStream = stream; //Thats where you put your stream! | |
[[[NSOperationQueue alloc] init] addOperation:operation]; | |
} |
As I told you earlier I’m converting the image to NSImputStream and assign it to the request operation object. And I have added folder name and the file name as request to herder fields.