MVC接收以post形式傳輸的各類參數

近日研究用wcf框架接收同事Android端以post形式傳輸的各類類型的參數,來作處理。但研究過程當中問題比較多,首先鍵值對的形式是實現了,傳輸int,string類型都不成問題,可是到傳輸文件的時候,以流stream的形式進行傳輸,遇到問題,通過研究,本人對wcf的知道理解有限,短期內達不到運用自如的狀態。後改成用mvc框架進行接收,在同事的協做與幫助下,經一番試驗,各類參數得以成功傳輸。數據庫

現將代碼整理以下(如下代碼通過測試,可成功運行):json

  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 }
相關文章
相關標籤/搜索