private JObject GetFormDataParameter(Stream stream, HttpRequest request) { request.Body = stream; FormFeature formFeature = new FormFeature(request, _formOptions); IFormCollection forms = formFeature.ReadForm(); if (forms == null || forms.Count == 0) { return(null); } JObject json = new JObject(); foreach (var f in forms) { json.Add(f.Key, f.Value.ToString()); } return(json); }
//解析接收的参数 private Dictionary <string, object> CreateDict(string method, out string auth) { auth = ""; Dictionary <string, object> dict = new Dictionary <string, object>(); if (method == "post") { if (context.Request.ContentType == "application/x-www-form-urlencoded") //纯表单 { foreach (KeyValuePair <string, StringValues> kv in context.Request.Form) { if (kv.Key == "__RequestVerificationToken") { continue; } if (!dict.ContainsKey(kv.Key)) { if (auth != "") { auth += "_"; } auth += kv.Key; dict.Add(kv.Key, kv.Value[0]); } } } else if (context.Request.ContentType.Contains("multipart/form-data", StringComparison.OrdinalIgnoreCase)) //带附件的表单 { FormFeature formFeature = new FormFeature(context.Request); IFormCollection iFormCollection = formFeature.ReadForm(); foreach (string key in iFormCollection.Keys) { if (!dict.ContainsKey(key)) { if (auth != "") { auth += "_"; } auth += key; StringValues stringValues = iFormCollection[key]; dict.Add(key, stringValues[0]); } } if (iFormCollection.Files.Count > 0) { IFormFile formFile = iFormCollection.Files[0]; //var target = new System.IO.MemoryStream(); //formFile.CopyTo(target); //byte[] buffer = target.ToArray(); dict.Add(formFile.Name, formFile); if (auth != "") { auth += "_"; } auth += formFile.Name; } } else { var reader = new StreamReader(context.Request.Body); var contentFromBody = reader.ReadToEnd(); if (contentFromBody != "") { dict = JsonConvert.DeserializeObject <Dictionary <string, object> >(contentFromBody); } foreach (KeyValuePair <string, object> kv in dict) { if (kv.Key != _ReqFilter) { if (auth != "") { auth += "_"; } auth += kv.Key; } } } } else if (method == "get") { dict = new Dictionary <string, object>(); foreach (KeyValuePair <string, StringValues> kv in context.Request.Query) { if (!dict.ContainsKey(kv.Key)) { if (auth != "") { auth += "_"; } auth += kv.Key; dict.Add(kv.Key, kv.Value[0]); } } } return(dict); }