近日研究用wcf框架接收同事Android端以post形式传输的各种类型的参数,来做处理。但研究过程中问题比较多,首先键值对的形式是实现了,传输int,string类型都不成问题,但是到传输文件的时候,以流stream的形式进行传输,遇到问题,经过研究,本人对wcf的知道理解有限,短时间内达不到运用自如的状态。后改为用mvc框架进行接收,在同事的协作与帮助下,经一番试验,各种参数得以成功传输。

现将代码整理如下(以下代码经过测试,可成功运行):

 using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Script.Serialization;
using System.Text;
using System.Collections; namespace PoliceAPP.Controllers
{
public class TestController : BaseController
{
//
// GET: /Test/ public string Index()
{
// (1) 解析参数
string json = "";
var hh = "";
// 接收对方文件类型的参数 "file"为参数名,必须和对方的参数名一致
var myfile = Request.Files["file"];
if (myfile != null)
{//文件保存路径
var filePath = Path.Combine(Request.MapPath("~/Upload"), Path.GetFileName(myfile.FileName));
if (Directory.Exists(filePath))
{ }
else
{
myfile.SaveAs(filePath);
}
}
//接收图片
var myfile1 = Request.Files["img"];
if (myfile1 != null)
{
myfile1.SaveAs(Path.Combine(Request.MapPath("~/Upload"), Path.GetFileName(myfile1.FileName)));
}
//接收多个文件(对方以数组形式传输)
var filelist = Request.Files.GetMultiple("filelist");
foreach (HttpPostedFileBase file in filelist)
{
//HttpPostedFileBase uploadFile = Request.Files[file] as HttpPostedFileBase;
if (file!= null && file.ContentLength > )
{
var filepath1 = Path.Combine(Request.MapPath("~/Upload"), Path.GetFileName(file.FileName));
file.SaveAs(filepath1);
}
} JavaScriptSerializer js = null;
Person p = new Person();
try
{ //接收值
json = Request["Age"];/// "data={Age:18,Name:"zhangxu"}"
hh = Request["Name"];
//ss = Request["File"];
System.Diagnostics.Debug.Assert(false, hh);
System.Diagnostics.Debug.Assert(false, json); js = new JavaScriptSerializer(); //实例化一个能够序列化数据的类
//Person list = js.Deserialize<Person>(json); //将json数据转化为对象类型并赋值给list
p = new Person();
p.Name = hh;//list.Name;
p.Age = string.IsNullOrEmpty(hh) ? : Convert.ToInt32(json);// list.Age;
}
catch (Exception)
{ System.Diagnostics.Debug.Assert(false, "yichang");
System.Diagnostics.Debug.Assert(false, Request.Params.ToString()); }
System.Diagnostics.Debug.Assert(false, Request.Params.ToString());
// 数据库逻辑 //
//Person p = new Person();
//p.Age = 9;
//p.Name = "zhangxu";
js.Serialize(p);
return js.Serialize(p); } public string ZX()
{
// (1) 解析参数
var json = Request["data"];/// "data={Age:18,Name:"zhangxu"}" JavaScriptSerializer js = new JavaScriptSerializer(); //实例化一个能够序列化数据的类
//Person list = js.Deserialize<Person>(json); //将json数据转化为对象类型并赋值给list
//string result = list.Name;
//var res_info = list.Age;
// 数据库逻辑 //
Person p = new Person();
p.Age = ;
p.Name = "ZX";
js.Serialize(p);
return js.Serialize(p); } }
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
}

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. 导出页面文档(只在IE8下测试过)

    之前说过一篇关于打印的方法,就顺便也看了一下导出,但是该方法需要用户更改浏览器的安全级别设置,因此并不十分推荐,大家如真有需要可以参考一下ZeroClipboard这款插件,我有时间也会去学习一下并贴 ...

  2. FMDB警告Warning: there is at least one open result set around after performing的问题

    FMDB操作sqlite的时候总是报警告Warning: there is at least one open result set around after performing,后来发现是执行查询 ...

  3. linux修改环境变量

    /etc/profile 系统全局环境变量设定,所有用户共享,修改后,需要重启系统才能生效 ~/.bash_profile,~/.bashrc 用户目录下的私有环境变量设定,常用来个性化定制功能,修改 ...

  4. Python数据类型list(列表)和tuple(元组)

    list Python内置的一种数据类型是列表:list.list是一种有序的集合,可以随时添加和删除其中的元素. 比如,列出班里所有同学的名字,就可以用一个list表示: >>> ...

  5. C语言结构体占用空间内存大小解析

    结构体的数据类型的有点我们就不啰嗦了,直接来看相同数据结构体的几种书写的格式吧. 格式一: 01.struct tagPhone 02.{ 03.     char   A; 04.     int  ...

  6. 重构前的程序:通过rsync命令抓取日志文件

    基本概况: 我有一台服务器每天每个小时都会生成一个日志文件,这些日志文件会被保留2天,超过2天会被一个程序压缩放到备份目录,日志文件的文件名是有命名要求的,例如:project_log.2013010 ...

  7. poj 3020Antenna Placement

    http://poj.org/problem?id=3020 #include<cstdio> #include<cstring> #include<algorithm& ...

  8. keil c编译器错误与解决方法

    1. Warning 280:’i’:unreferenced local variable 说明局部变量i 在函数中未作任何的存取操作解决方法消除函数中i 变量的宣告 2 Warning 206:’ ...

  9. 使用Qt编写模块化插件式应用程序

    动态链接库技术使软件工程师们兽血沸腾,它使得应用系统(程序)可以以二进制模块的形式灵活地组建起来.比起源码级别的模块化,二进制级别的模块划分使得各模块更加独立,各模块可以分别编译和链接,模块的升级不会 ...

  10. 新版的DEV RichEdit很强悍,兼容docx,排版更强

    RV至少rtf格式不用自己搞了 Rv没Dev出的强悍 RV最蛋疼的就是表格 DEV目前看来,表格比RV强其他方面来说,觉得到差不多,无所谓dev的excel我整过一次,BUG不少dxRichEdit换 ...