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

筛选器:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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")]

):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
[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);
      }

  

posted @   数据酷软件  阅读(815)  评论(0编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
点击右上角即可分享
微信分享提示