直接上干货了

1:WebApi后端代码

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Threading.Tasks;
  5 
  6 namespace ZRFCoreTestMongoDB.Controllers
  7 {
  8     using Microsoft.AspNetCore.Authorization;
  9     using Microsoft.AspNetCore.Mvc.Filters;
 10     using Microsoft.AspNetCore.Mvc;
 11     using ZRFCoreTestMongoDB.Model;
 12     using ZRFCoreTestMongoDB.Commoms;
 13 
 14 
 15     using Microsoft.IdentityModel.Tokens;
 16     using System.Text;
 17     using System.Security.Claims;
 18     using System.IdentityModel.Tokens.Jwt;
 19     using Microsoft.AspNetCore.Http;
 20     using System.IO;
 21     using Microsoft.AspNetCore.Hosting;
 22 
 23 
 24     /// <summary>
 25     /// 
 26     /// </summary>
 27     [ApiController]
 28     [Route("api/[Controller]")]
 29     public class MyJwtTestController : ControllerBase
 30     {
 31         private readonly JwtConfigModel _jsonmodel;
 32         private IWebHostEnvironment _evn;
 33         public MyJwtTestController(IWebHostEnvironment evn)
 34         {
 35             this._evn = evn;
 36             _jsonmodel = AppJsonHelper.InitJsonModel();
 37         }
 38         
 39         #region CoreApi文件上传
 40 
 41         [HttpPost, Route("UpFile")]
 42         public async Task<ApiResult> UpFile([FromForm]IFormCollection formcollection)
 43         {
 44             ApiResult result = new ApiResult();
 45             try
 46             {
 47                 var files = formcollection.Files;//formcollection.Count > 0 这样的判断为错误的
 48                 if (files != null && files.Any())
 49                 {
 50                     var file = files[0];
 51                     string contentType = file.ContentType;
 52                     string fileOrginname = file.FileName;//新建文本文档.txt  原始的文件名称
 53                     string fileExtention = Path.GetExtension(fileOrginname);
 54                     string cdipath = Directory.GetCurrentDirectory();
 55 
 56                     string fileupName = Guid.NewGuid().ToString("d") + fileExtention;
 57                     string upfilePath = Path.Combine(cdipath + "/myupfiles/", fileupName);
 58                     if (!System.IO.File.Exists(upfilePath))
 59                     {
 60                         using var Stream = System.IO.File.Create(upfilePath);
 61                     }
 62 
 63                     #region MyRegion
 64                     //using (FileStream fileStream = new FileStream(upfilePath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite))
 65                     //{
 66                     //    using (Stream stream = file.OpenReadStream()) //理论上这个方法高效些
 67                     //    {
 68                     //        await stream.CopyToAsync(fileStream);//ok
 69                     //        result.message = "上传成功!"; result.code = statuCode.success;
 70                     //    }
 71                     //}
 72 
 73                     //using (FileStream fileStream = new FileStream(upfilePath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite))
 74                     //{
 75                     //    await file.CopyToAsync(fileStream);//ok
 76                     //    result.message = "上传成功!"; result.code = statuCode.success;
 77                     //}
 78 
 79                     #endregion
 80 
 81                     double count = await UpLoadFileStreamHelper.UploadWriteFileAsync(file.OpenReadStream(), upfilePath);
 82                     result.message = "上传成功!"; result.code = statuCode.success; result.data = $"上传的文件大小为:{count}MB";
 83                 }
 84             }
 85             catch (Exception ex)
 86             {
 87                 result.message = "上传异常,原因:" + ex.Message;
 88             }
 89             return result;
 90 
 91         }
 92 
 93         /// <summary>
 94         /// 文件上传 [FromForm]需要打上这个特性
 95         /// </summary>
 96         /// <param name="model">上传的字段固定为: file</param>
 97         /// <returns></returns>
 98 
 99         [HttpPost, Route("UpFile02")]
100         public async Task<ApiResult> UpFileBymodel([FromForm]UpFileModel model)// 这里一定要加[FromForm]的特性,模型里面可以不用加
101         {
102             ApiResult result = new ApiResult();
103             try
104             {
105                 bool falg = await UpLoadFileStreamHelper.UploadWriteFileByModelAsync(model);
106                 result.code = falg ? statuCode.success : statuCode.fail;
107                 result.message = falg ? "上传成功" : "上传失败";
108             }
109             catch (Exception ex)
110             {
111                 result.message = "上传异常,原因:" + ex.Message;
112             }
113             return result;
114         }
115 
116         [HttpPost, Route("UpFile03")]
117         public async Task<ApiResult> UpFile03(IFormFile file)// 这里一定要加[FromForm]的特性,模型里面可以不用加
118         {
119             ApiResult result = new ApiResult();
120             try
121             {
122                 bool flag = await UpLoadFileStreamHelper.UploadWriteFileAsync(file);
123                 result.code = flag ? statuCode.success : statuCode.fail;
124                 result.message = flag ? "上传成功" : "上传失败";
125             }
126             catch (Exception ex)
127             {
128                 result.message = "上传异常,原因:" + ex.Message;
129             }
130             return result;
131         }
132         #endregion
133     }
134 }
View Code

相关文章:

  • 2022-12-23
  • 2021-05-29
  • 2021-09-28
  • 2022-12-23
  • 2022-12-23
  • 2022-01-05
  • 2021-11-13
猜你喜欢
  • 2021-05-23
  • 2021-07-10
  • 2022-01-06
  • 2022-12-23
  • 2022-12-23
  • 2021-11-25
  • 2021-09-03
相关资源
相似解决方案