Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>To send an image down to a WCF REST service, using ASIFormDataRequest.. here is an example from a project we have in production...</p> <p>assumes I have a UIImage in a var called 'image'</p> <pre><code>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]; </code></pre> <p>OK, on the WCF side, you need to define a method that receives a System.IO.Stream, and that Stream needs to be the last parameter defined, it sould be a POST, and should not contain any other parameters as part of the POST body (you can define parameters in the URL and query string, although some purists would say that that's bad form for a REST POST).</p> <pre><code>[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)) &gt; 0) { ms.Write(buffer, 0, read); } ms.Position = 0; if (ms.Length &gt; 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; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload