asp.net mvc接收安卓post的json字符串

时间:2022-06-05 09:55:21

筛选器:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Runtime.Serialization.Json; namespace BP_RFID_WMS.Controllers
{
/// <summary>
/// Request.InputStream流筛选器
/// </summary>
public class JsonStringFilter : ActionFilterAttribute
{
/// <summary>
/// 参数
/// </summary>
public string Param
{
get;
set;
} /// <summary>
/// 接受Request.InputStream流的POST数据Encoding为utf-8编码的字符串
/// </summary>
/// <param name="filterContext"></param>
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
if ((filterContext.HttpContext.Request.ContentType ?? string.Empty).Contains("application/json"))
{
try
{
byte[] byts = new byte[filterContext.HttpContext.Request.InputStream.Length];
filterContext.HttpContext.Request.InputStream.Read(byts, 0, byts.Length);
string req = System.Text.Encoding.UTF8.GetString(byts);
req = filterContext.HttpContext.Server.UrlDecode(req);
filterContext.ActionParameters[Param] = req;
}
catch (Exception ex)
{
Com.DataCool.DotNetExpand.LogHelper.Error(ex);
}
}
}
}
}

  Controller(注意写法,写个标签就OK了 [JsonStringFilter(Param = "entity")]

):

  [JsonStringFilter(Param = "entity")]
public JsonResult SaveGridDataToExcelFile(string entity)
{
if (!string.IsNullOrEmpty(entity))
{
JObject jsonParams = JObject.Parse(entity);
var gridRows = (JArray)jsonParams["GridRows"];
var gridOptions = (JArray)jsonParams["GridColumnOptions"];
var gridOptionList = (JArray)gridOptions;
//可见的列
List<JObject> gridCols = new List<JObject>();
foreach (JObject j in gridOptionList)
{
if (j.ToString().IndexOf("hidden") == -1)
{
gridCols.Add(j);
}
}
var fileName = jsonParams["ExportFileName"].Value<string>();
string tempFileName = HttpContext.Server.MapPath("~/") + "TemplateFiles\\" + "CommonExcelFile.xls";
FileStream fs = new FileStream(tempFileName, FileMode.Open, FileAccess.Read);
var workBook = new HSSFWorkbook(fs);
workBook.SetSheetName(0, "sheet1");
var sheet = workBook.GetSheetAt(0);
//表头(列),第一行
int newColIndex = 0;
var titleRow = sheet.CreateRow(newColIndex);
int cIndex = 0;
foreach (JObject j in gridCols)
{
titleRow.CreateCell(cIndex).SetCellValue(j["title"].Value<String>());
int width = j["width"].Value<int>() / 6;
if (width > 255)
width = 250;
sheet.SetColumnWidth(cIndex, width * 256);
cIndex++;
}
//行记录
for (int rowIndex = 0; rowIndex < gridRows.Count; rowIndex++)
{
newColIndex++;
var row = sheet.CreateRow(newColIndex);
var jsonEntity = gridRows[rowIndex] as JObject;
for (int colIndex = 0; colIndex < gridCols.Count; colIndex++)
{
string cellValue = string.Empty;
JObject colOption = (JObject)gridCols[colIndex];
string field = colOption["field"].Value<string>();
if (jsonEntity[field].ToString().Length != 0)
cellValue = jsonEntity[field].Value<String>();
row.CreateCell(colIndex).SetCellValue(cellValue);
}
}
MemoryStream newFile = new MemoryStream();
sheet.Workbook.Write(newFile);
using (Reserve_DbEntities db = new Reserve_DbEntities())
{
var resultFile = new AppExportFile();
resultFile.FileGuid = Guid.NewGuid();
resultFile.FileName = fileName;
resultFile.FileCreateDateTime = DateTime.Now;
resultFile.FileStreamByte = newFile.GetBuffer();
db.AddToAppExportFile(resultFile);
db.SaveChanges();
var data = new { fileID = resultFile.FileGuid.ToString() };
return Json(data, JsonRequestBehavior.AllowGet);
}
}
else return Json(string.Empty, JsonRequestBehavior.AllowGet);
}