近日研究用wcf框架接收同事Android端以post形式传输的各种类型的参数,来做处理。但研究过程中问题比较多,首先键值对的形式是实现了,传输int,string类型都不成问题,但是到传输文件的时候,以流stream的形式进行传输,遇到问题,经过研究,本人对wcf的知道理解有限,短时间内达不到运用自如的状态。后改为用mvc框架进行接收,在同事的协作与帮助下,经一番试验,各种参数得以成功传输。
现将代码整理如下(以下代码经过测试,可成功运行):
1 using System; 2 using System.Collections.Generic; 3 using System.IO; 4 using System.Linq; 5 using System.Web; 6 using System.Web.Mvc; 7 using System.Web.Script.Serialization; 8 using System.Text; 9 using System.Collections; 10 11 namespace PoliceAPP.Controllers 12 { 13 public class TestController : BaseController 14 { 15 // 16 // GET: /Test/ 17 18 19 public string Index() 20 { 21 // (1) 解析参数 22 string json = ""; 23 var hh = ""; 24 // 接收对方文件类型的参数 "file"为参数名,必须和对方的参数名一致 25 var myfile = Request.Files["file"]; 26 if (myfile != null) 27 {//文件保存路径 28 var filePath = Path.Combine(Request.MapPath("~/Upload"), Path.GetFileName(myfile.FileName)); 29 if (Directory.Exists(filePath)) 30 { 31 32 } 33 else 34 { 35 myfile.SaveAs(filePath); 36 } 37 } 38 //接收图片 39 var myfile1 = Request.Files["img"]; 40 if (myfile1 != null) 41 { 42 myfile1.SaveAs(Path.Combine(Request.MapPath("~/Upload"), Path.GetFileName(myfile1.FileName))); 43 } 44 //接收多个文件(对方以数组形式传输) 45 var filelist = Request.Files.GetMultiple("filelist"); 46 foreach (HttpPostedFileBase file in filelist) 47 { 48 //HttpPostedFileBase uploadFile = Request.Files[file] as HttpPostedFileBase; 49 if (file!= null && file.ContentLength > 0) 50 { 51 var filepath1 = Path.Combine(Request.MapPath("~/Upload"), Path.GetFileName(file.FileName)); 52 file.SaveAs(filepath1); 53 } 54 } 55 56 JavaScriptSerializer js = null; 57 Person p = new Person(); 58 try 59 { //接收值 60 json = Request["Age"];/// "data={Age:18,Name:"zhangxu"}" 61 hh = Request["Name"]; 62 //ss = Request["File"]; 63 System.Diagnostics.Debug.Assert(false, hh); 64 System.Diagnostics.Debug.Assert(false, json); 65 66 js = new JavaScriptSerializer(); //实例化一个能够序列化数据的类 67 //Person list = js.Deserialize<Person>(json); //将json数据转化为对象类型并赋值给list 68 p = new Person(); 69 p.Name = hh;//list.Name; 70 p.Age = string.IsNullOrEmpty(hh) ? 0 : Convert.ToInt32(json);// list.Age; 71 } 72 catch (Exception) 73 { 74 75 System.Diagnostics.Debug.Assert(false, "yichang"); 76 System.Diagnostics.Debug.Assert(false, Request.Params.ToString()); 77 78 } 79 System.Diagnostics.Debug.Assert(false, Request.Params.ToString()); 80 // 数据库逻辑 81 82 // 83 //Person p = new Person(); 84 //p.Age = 9; 85 //p.Name = "zhangxu"; 86 js.Serialize(p); 87 return js.Serialize(p); 88 89 } 90 91 92 public string ZX() 93 { 94 // (1) 解析参数 95 var json = Request["data"];/// "data={Age:18,Name:"zhangxu"}" 96 97 98 JavaScriptSerializer js = new JavaScriptSerializer(); //实例化一个能够序列化数据的类 99 //Person list = js.Deserialize<Person>(json); //将json数据转化为对象类型并赋值给list 100 //string result = list.Name; 101 //var res_info = list.Age; 102 // 数据库逻辑 103 104 // 105 Person p = new Person(); 106 p.Age = 9; 107 p.Name = "ZX"; 108 js.Serialize(p); 109 return js.Serialize(p); 110 111 } 112 113 114 } 115 public class Person 116 { 117 public string Name { get; set; } 118 public int Age { get; set; } 119 } 120 }