在net6的api接口中,有的参数是文件类型,读取文件的内容。
1 /// <summary> 2 /// 导入 3 /// </summary> 4 /// <returns></returns> 5 [HttpPost("Upload")] 6 public ActionResult<APIResult<List<APIResult<Guid>>>> Upload() 7 { 8 APIResult<List<APIResult<Guid>>> result = new APIResult<List<APIResult<Guid>>>(); 9 result.Response = new List<APIResult<Guid>>(); 10 result.IsOk = true; 11 try 12 { 13 string readString = ""; 14 IFormFileCollection files = HttpContext.Request.Form.Files; 15 if (files.Count > 0) 16 { 17 using (Stream stream = files[0].OpenReadStream()) 18 { 19 stream.Position = 0; 20 StreamReader reader = new StreamReader(stream); 21 readString = reader.ReadToEnd(); 22 } 23 } 24 if (string.IsNullOrEmpty(readString)) 25 throw new Exception("导入文件不能为空"); 26 } 27 catch (Exception ex) 28 {} 31 return result; 32 }
标签:files,读取,stream,api,result,new,net6,readString From: https://www.cnblogs.com/BH-RJ-WK/p/16824107.html