IOS WCF

为某项目做的前期调查。目前可以实现检索,登录等相关机能,部分实现上传文件机能。上传文件仍有大小限制的问题,文件较大时,会出现上传失败的现象(2MB左右的文件会上传失败,),原因暂时不清楚。

项目内调查结果如下:WCF服务端:1.定义WCF协议接口(Interface)


 
 
[csharp] view plain copy
  1. <pre name="code" class="csharp">[ServiceContract]  
  2. public interface IUpLoadService  
  3. {  
  4.     [OperationContract]  
  5.     [WebInvoke(Method = "POST", UriTemplate = "UploadFile/{fileName}")]  
  6.     void UploadFile(string fileName, Stream fileContent);  
  7.   
  8.     [OperationContract]    
  9.     [WebGet(UriTemplate = "GetImageInfo/{name}", ResponseFormat = WebMessageFormat.Json)]    
  10.     ImageInfo GetImageInfo(string name);  
  11. }</pre>  

2.实现协议接口


  
  
[csharp] view plain copy
  1. public class UpLoadService : IUpLoadService  
  2. {  
  3.     public void UploadFile(string fileName, Stream fileContent)  
  4.     {  
  5.         FileStream fs = new FileStream("D:\\" + fileName, FileMode.OpenOrCreate);  
  6.         try  
  7.         {  
  8.             BinaryReader reader = new BinaryReader(fileContent);  
  9.             byte[] buffer;  
  10.             BinaryWriter writer = new BinaryWriter(fs);  
  11.             long offset = fs.Length;  
  12.             writer.Seek((int)offset, SeekOrigin.Begin);  
  13.             do  
  14.             {  
  15.                 buffer = reader.ReadBytes(1024);  
  16.                 writer.Write(buffer);  
  17.             } while (buffer.Length > 0);  
  18.         }  
  19.         catch (Exception e)  
  20.         {  
  21.         }  
  22.         finally  
  23.         {  
  24.             fs.Close();  
  25.             fileContent.Close();  
  26.         }  
  27.     }  
  28.   
  29.     public ImageInfo GetImageInfo(string name) {  
  30.         return new ImageInfo {imageName=name, imageSize="122KB"};  
  31.     }  
  32. }  

3.Service配置文件:App.config

配置文件中,需要在自己service的endpoint节点中指定绑定方式binding="basicHttpBinding" ,
以及绑定设置bindingConfiguration="MyServiceBinding",并在bindings节点中定义相应的basicHttpBinding的绑定设置。
这主要是为了设置上传文件的大小限制。
例如:

  
  
[html] view plain copy
  1. <pre name="code" class="html"><binding name="DocumentExplorerServiceBinding" sendTimeout="00:10:00" transferMode="Streamed" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647" maxBufferPoolSize="2147483647" >  
  2.     <readerQuotas maxDepth="32" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="4096" maxNameTableCharCount="16384" />  
  3.     <security mode="None" />  
  4. </binding></pre>  

iOS端程序:

1.上传文件


   
   
[csharp] view plain copy
  1. NSURL *url = [NSURL URLWithString:@"http://172.16.xxx.xxx:82/Service.svc/UploadFile/myphoto.png"];  
  2. request_ = [ASIFormDataRequest requestWithURL:url];  
  3. [request_ setPostValue:@"myphoto1.png" forKey:@"fileName"];  
  4. UIImage *image = [UIImage imageNamed:@"myphoto1.png"];  
  5. NSData* imageData = [[NSData alloc] initWithData:UIImagePNGRepresentation(image)];  
  6. [request_ appendPostData:imageData];  
  7. [request_ setRequestMethod:@"POST"];  
  8. [request_ setDidFinishSelector:@selector(requestFinished:)];  
  9. [request_ setDidFailSelector:@selector(requestFailed:)];  
  10. [request_ setDelegate:self];  
  11. [request_ startAsynchronous];  

2.调用方法取得返回值

[csharp] view plain copy
  1. NSURL *url = [NSURL URLWithString:@"http://172.16.xxx.xxx:82/Service.svc/GetImageInfo/1.jpg"];    
  2. ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];    
  3. [request startSynchronous];    
  4. NSError *error = [request error];    
  5. if (!error) {    
  6.     NSString *response = [request responseString];   
  7.     NSLog(response);   

http://blog.csdn.net/musou_ldns/article/details/6933006

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值