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

筛选器:

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);
        }

  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值