简体   繁体   English

使用ASIFormDataRequest时如何实现C#服务器端?

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

I am trying to use ASIFormDataRequest to send data to ASP.net server side.我正在尝试使用 ASIFormDataRequest 将数据发送到 ASP.net 服务器端。 I have created an aspx page.Currently I can get the two plain text.我已经创建了一个 aspx 页面。目前我可以得到这两个纯文本。 Howerver I don't know how to prase the NSdata in C# by Request.Form.但是我不知道如何通过 Request.Form 在 C# 中传递 NSdata。

Here is Obj-C code:这是 Obj-C 代码:

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:这是当前 C# 代码:

 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?如您所见,在 iphone 中,我尝试将图像发送到 C# 服务器端,但我不知道该怎么做?

To send an image down to a WCF REST service, using ASIFormDataRequest.. here is an example from a project we have in production...要将图像发送到 WCF REST 服务,使用 ASIFormDataRequest .. 这是我们在生产中的项目的示例...

assumes I have a UIImage in a var called 'image'假设我在一个名为“image”的变量中有一个 UIImage

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). 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 正文(您可以在 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;
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在服务器端C#上实现套接字? - How to implement sockets on the server side C#? 如何使用 Stripe 支付网关在 C# (dot.net web api) 的服务器端实现 Google Pay? - How to implement Google Pay in Server Side in C# (dot net web api) using Stripe Payment Gateway? 如何使用 C#、ASP.NET、SQL Server 端处理实现 jQuery DataTables 插件? - How can I implement jQuery DataTables plugin using C#, ASP.NET, SQL Server side processing? 我应该如何为ajax请求实现长轮询的C#服务器端部分? - How should I implement the C# server side portion of long-polling for ajax requests? 如何使用C#和实体框架实现SQL Server分页? - How to implement SQL Server paging using C# and entity framework? 如何在客户端(使用javascript)和服务器端验证mac地址和ipaddress(使用c#) - How to Validate mac address & ipaddress in client side(using javascript) & server side(using c#) 使用来自客户端 C# 的服务器端 PHP 代码 - Using server side PHP code from client side C# 如何使用 Jquery 客户端或 C# 服务器端验证泛卡号格式 - How to Validate Pan Card Number Format Using Jquery Client Side Or C# Server side 如何实现C#自定义服务器控件? - How to implement a C# custom server control? 如何在C#中实现OLE服务器 - How to implement OLE server in C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM