近日研究用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. public string Index()
  19. {
  20. // (1) 解析参数
  21. string json = "";
  22. var hh = "";
  23. // 接收对方文件类型的参数 "file"为参数名,必须和对方的参数名一致
  24. var myfile = Request.Files["file"];
  25. if (myfile != null)
  26. {//文件保存路径
  27. var filePath = Path.Combine(Request.MapPath("~/Upload"), Path.GetFileName(myfile.FileName));
  28. if (Directory.Exists(filePath))
  29. {
  30.  
  31. }
  32. else
  33. {
  34. myfile.SaveAs(filePath);
  35. }
  36. }
  37. //接收图片
  38. var myfile1 = Request.Files["img"];
  39. if (myfile1 != null)
  40. {
  41. myfile1.SaveAs(Path.Combine(Request.MapPath("~/Upload"), Path.GetFileName(myfile1.FileName)));
  42. }
  43. //接收多个文件(对方以数组形式传输)
  44. var filelist = Request.Files.GetMultiple("filelist");
  45. foreach (HttpPostedFileBase file in filelist)
  46. {
  47. //HttpPostedFileBase uploadFile = Request.Files[file] as HttpPostedFileBase;
  48. if (file!= null && file.ContentLength > )
  49. {
  50. var filepath1 = Path.Combine(Request.MapPath("~/Upload"), Path.GetFileName(file.FileName));
  51. file.SaveAs(filepath1);
  52. }
  53. }
  54.  
  55. JavaScriptSerializer js = null;
  56. Person p = new Person();
  57. try
  58. { //接收值
  59. json = Request["Age"];/// "data={Age:18,Name:"zhangxu"}"
  60. hh = Request["Name"];
  61. //ss = Request["File"];
  62. System.Diagnostics.Debug.Assert(false, hh);
  63. System.Diagnostics.Debug.Assert(false, json);
  64.  
  65. js = new JavaScriptSerializer(); //实例化一个能够序列化数据的类
  66. //Person list = js.Deserialize<Person>(json); //将json数据转化为对象类型并赋值给list
  67. p = new Person();
  68. p.Name = hh;//list.Name;
  69. p.Age = string.IsNullOrEmpty(hh) ? : Convert.ToInt32(json);// list.Age;
  70. }
  71. catch (Exception)
  72. {
  73.  
  74. System.Diagnostics.Debug.Assert(false, "yichang");
  75. System.Diagnostics.Debug.Assert(false, Request.Params.ToString());
  76.  
  77. }
  78. System.Diagnostics.Debug.Assert(false, Request.Params.ToString());
  79. // 数据库逻辑
  80.  
  81. //
  82. //Person p = new Person();
  83. //p.Age = 9;
  84. //p.Name = "zhangxu";
  85. js.Serialize(p);
  86. return js.Serialize(p);
  87.  
  88. }
  89.  
  90. public string ZX()
  91. {
  92. // (1) 解析参数
  93. var json = Request["data"];/// "data={Age:18,Name:"zhangxu"}"
  94.  
  95. JavaScriptSerializer js = new JavaScriptSerializer(); //实例化一个能够序列化数据的类
  96. //Person list = js.Deserialize<Person>(json); //将json数据转化为对象类型并赋值给list
  97. //string result = list.Name;
  98. //var res_info = list.Age;
  99. // 数据库逻辑
  100.  
  101. //
  102. Person p = new Person();
  103. p.Age = ;
  104. p.Name = "ZX";
  105. js.Serialize(p);
  106. return js.Serialize(p);
  107.  
  108. }
  109.  
  110. }
  111. public class Person
  112. {
  113. public string Name { get; set; }
  114. public int Age { get; set; }
  115. }
  116. }

MVC接收以post形式传输的各种参数的更多相关文章

  1. 使用Spring mvc接收整个url地址及参数时注意事项

    使用Spring mvc接收整个url地址及参数时注意事项:url= http://baidu?oid=9525c1f2b2cd45019b30a37bead6ebbb&td=2015-08- ...

  2. spring mvc接收ajax提交的JSON数据,并反序列化为对象

    需求:spring mvc接收ajax提交的JSON数据,并反序列化为对象,代码如下: 前台JS代码: //属性要与带转化的对象属性对应 var param={name:'语文',price:16}; ...

  3. ajax post提交空字符串(string.Empty) MVC接收为null的问题

    ajax post提交空字符串(string.Empty) MVC接收为null的问题 这个问题查了好多资料才知道原因: if (bindingContext.ModelMetadata.Conver ...

  4. spring mvc接收数组

    (一)前言 对于springmvc接收数组的问题啊,我试验过几次,但是了有时候成功了,有时候失败了,也不知道为啥的,然后现在又要用到了,所以打算具体看看到底怎么回事,但是了我实验成功了顺便找了好多资料 ...

  5. 【spring mvc】后台spring mvc接收List参数报错如下:org.springframework.beans.BeanInstantiationException: Failed to instantiate [java.util.List]: Specified class is an interface

    后台spring mvc接收List参数报错如下:org.springframework.beans.BeanInstantiationException: Failed to instantiate ...

  6. 关于“在从服务器接收结果时发生传输级错误。 (provider: TCP Provider, error: 0 - 指定的网络名不再可用。)”的解决方法之一

    最近几天发现连sql数据库服务器的时候,总是提示“在从服务器接收结果时发生传输级错误. (provider: TCP Provider, error: 0 - 指定的网络名不再可用.)”的错误. 网上 ...

  7. Error-ASP.NET:在从服务器接收结果时发生传输级错误。 (provider: Session Provider, error: 19 - 物理连接不可用)

    ylbtech-Error-ASP.NET:在从服务器接收结果时发生传输级错误. (provider: Session Provider, error: 19 - 物理连接不可用)  1.返回顶部 1 ...

  8. Spring MVC接收参数(Map,List,JSON,Date,2个Bean)(记录一次面试惨状)

    题目Spring MVC 接收参数 MapListDate2个BeanJSON Spring MVC接收参数 -Map Spring MVC接收参数 -List Spring MVC接收参数 -dat ...

  9. SPRING IN ACTION 第4版笔记-第五章BUILDING SPRING WEB APPLICATIONS-004-以query parameters的形式给action传参数(@RequestParam、defaultValue)

    一. 1.Spring MVC provides several ways that a client can pass data into a controller’s handler method ...

随机推荐

  1. 46 Permutations(全排列Medium)

    题目意思:全排列 思路:其实看这题目意思,是不太希望用递归的,不过还是用了递归,非递归的以后再搞吧 ps:vector这玩意不能随便返回,开始递归方法用vector,直接到500ms,换成void,到 ...

  2. html中插入flash代码详解(转载)

    <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://down ...

  3. hdu 1241 Oil Deposits(DFS求连通块)

    HDU 1241  Oil Deposits L -DFS Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & ...

  4. iOS开发——OC篇&消息传递机制(KVO/NOtification/Block/代理/Target-Action)

     iOS开发中消息传递机制(KVO/NOtification/Block/代理/Target-Action)   今晚看到了一篇好的文章,所以就搬过来了,方便自己以后学习 虽然这一期的主题是关于Fou ...

  5. openssl 进行证书格式的转换

    各类证书由于存储的内容不同(如是否包含公钥/私钥是否加密存储/单一证书或多证书等).采用编码不同(DER/BASE64).标准不同(如PEM/PKCS),所以尽管X.509标准规定了证书内容规范,但证 ...

  6. zoj 3785 What day is that day?

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5272 打表找规律. #include <cstdio> #incl ...

  7. 三个QT咨询公司以及QT5.0的主要特点

    三个咨询公司(他们也贡献代码):http://www.kdab.com/http://v-play.net/http://www.ics.com/qt 一个论坛:http://forum.qt.io/ ...

  8. Visual Studio如何删除多余的空行

    原文:Visual Studio如何删除多余的空行 如何在Visual  Studio中删除多余的空格: 适用于:Visual Studio2008 &2010 1.       Ctrl + ...

  9. PL/SQL 动态SQL

    declare msql varchar2(200); row_id varchar2(200); begin loop row_id := 'AAATGiAAEAAAuLLAAA'; msql := ...

  10. db file sequential read等待事件的一点研究

    db file sequential read等待事件有3个参数:file#,first block#,和block数量. 这个等待事件有3个参数P1,P2,P3, 其中P1代表Oracle要读取的文 ...