近日研究用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. struts2整合spring的思路

    struts2整合spring有有两种策略: >sping容器负责管理控制器Action,并利用依赖注入为控制器注入业务逻辑组件. >利用spring的自动装配,Action将自动会从Sp ...

  2. poj 1195Mobile phones

    http://poj.org/problem?id=1195 #include <cstdio> #include <cstring> #include <algorit ...

  3. 【HDOJ】4278 Faulty Odomete

    水题. /* 4278 */ #include <cstdio> #include <cstring> #include <cstdlib> #define MAX ...

  4. FFT多项式乘法加速

    FFT基本操作...讲解请自己看大学信号转置系列... 15-5-30更新:改成结构体的,跪烂王学长啊啊啊啊机智的static... #include<iostream> #include ...

  5. at命令

    在windows系统中,windows提供了计划任务这一功能,在控制面板 -> 性能与维护 -> 任务计划, 它的功能就是安排自动运行的任务. 通过'添加任务计划'的一步步引导,则可建立一 ...

  6. Java---练习(面试题) :字符串截取(1)

    在java中,字符串"abcd"与字符串"ab你好"的长度是一样,都是四个字符. 但对应的字节数不同,一个汉字占两个字节. 定义一个方法,按照指定的字节数来取子 ...

  7. Visual Studio创建跨平台移动应用_03.AppBuilder Extension

    1 背景 本章节是关于Telerik AppBuilder for Visual Studio的. 目前(2014.12)为Telerik公司Telerik Platform的一部分,Telerik ...

  8. 将多个图片整合到一张图片中再用CSS 进行网页背景定位

    原文地址:http://wenku.baidu.com/link?url=hj_qM9kmdMrg8KWXFD2bCF_uuJCxKJRvG97CkWk3itsPq3izMzfrKvSZYBzDGyP ...

  9. @RestController

    /* * Copyright 2002-2014 the original author or authors. * * Licensed under the Apache License, Vers ...

  10. Effective C++ 第二版 40)分层 41)继承和模板 42)私有继承

    条款40 通过分层来体现"有一个"或"用...来实现" 使某个类的对象成为另一个类的数据成员, 实现将一个类构筑在另一个类之上, 这个过程称为 分层Layeri ...