简体   繁体   中英

How to implement the C# server side when using ASIFormDataRequest?

I am trying to use ASIFormDataRequest to send data to ASP.net server side. I have created an aspx page.Currently I can get the two plain text. Howerver I don't know how to prase the NSdata in C# by Request.Form.

Here is Obj-C code:

ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setPostValue:@"Ben" forKey:@"name"];
[request setPostValue:@"Copsey" forKey:@"code"];
[request setFile:@"/Users/ben/Desktop/ben.jpg" forKey:@"photo"];
[request setData:imageData withFileName:@"myphoto.jpg" andContentType:@"image/jpeg" forKey:@"photo"];

This is Current C# code:

 string name = Request.Form["name"] == null ? "" : Request.Form["name"];
 string code = Request.Form["code"]==null?"":Request.Form["code"];

As you see,in iphone, I try to send an image to C# server side, but I don't know how do it?

To send an image down to a WCF REST service, using ASIFormDataRequest.. here is an example from a project we have in production...

assumes I have a UIImage in a var called '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];

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).

[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;
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM