Чтобы отправить изображение в службу WCF REST, используя ASIFormDataRequest .. Вот пример из проекта, который у нас находится в производстве ...
предполагает, что у меня есть UIImage в переменной с именем 'image'
NSString *surl = @"http:www.SomeRestService.com"
NSURL *url = [NSURL URLWithString:surl];
ASIFormDataRequest *r = [ASIFormDataRequest requestWithURL:url];
[r setValidatesSecureCertificate:NO];
[r setTimeOutSeconds:30];
[r setRequestMethod:@"POST"]; //default is POST (insert),
[r setDelegate:self];
[r setDidFailSelector:@selector(requestDidFail:)];
//[r addRequestHeader:@"Content-Type" value:@"application/json"] this will cause the call to fail. No content-type header for this call.
NSMutableData *imageData = [NSMutableData dataWithData:UIImageJPEGRepresentation(image, .35)]; //we are really compressing our images.. you can do what you want, of course.
[r setPostBody:imageData];
[r setDidFinishSelector:@selector(imageSaveDidFinish:)];
[r startAsynchronous];
ОК, на стороне WCF вам нужно определить метод, который получает System.IO.Stream, и что Stream должен быть последним определенным параметром, это должен быть POST, и он долженне содержит никаких других параметров как часть тела POST (вы можете определить параметры в URL и строке запроса, хотя некоторые пуристы скажут, что это плохая форма для REST POST).
[WebInvoke(UriTemplate = "Upload", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare, Method = "POST")]
public GenericObject SaveReceiptImage(System.IO.Stream imageStream)
{
try
{
byte[] buffer = new byte[16 * 1024];
using (MemoryStream ms = new MemoryStream())
{
int read = 0;
while ((read = imageStream.Read(buffer, 0, buffer.Length)) > 0)
{
ms.Write(buffer, 0, read);
}
ms.Position = 0;
if (ms.Length > 0)
{
//save your byte array to where you want
}
else
{
// woops, no image was passed in
}
}
}
catch (Exception ex)
{
//bad error occured, log it
}
return whatever;
}