近日研究用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 }