123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- using log4net;
- using Microsoft.AspNetCore.Http;
- using Microsoft.AspNetCore.Mvc;
- using Microsoft.Extensions.Logging;
- using NPOI.SS.Formula.Functions;
- using Ropin.Inspection.Api.Common;
- using Ropin.Inspection.Common.Accessor.Interface;
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Threading.Tasks;
- namespace Ropin.Inspection.Api.Controllers
- {
- public class FileController : BaseController
- {
- private static readonly ILog log = LogManager.GetLogger(typeof(FileController));
- private readonly ILogger<FileController> _logger;
- private readonly IClaimsAccessor _claims;
- /// <summary>
- /// 构造函数
- /// </summary>
- /// <param name="logger"></param>
- /// <param name="claims"></param>
- public FileController(ILogger<FileController> logger, IClaimsAccessor claims)
- {
- _logger = logger;
- _claims = claims;
- }
- /// <summary>
- /// 上传文件
- /// </summary>
- /// <param name="Files"></param>
- /// <returns></returns>
- [HttpPost("UploadFileAsync")]
- public async Task<ApiResult> UploadFileAsync(IFormCollection Files)
- {
- log.Info($"[开始] 上传文件信息.【{DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss:fff")}】");
- //_logger.LogInformation($"[开始] 上传文件信息.【{DateTime.Now}】");
- try
- {
- //var form = Request.Form;//直接从表单里面获取文件名不需要参数
- string dd = Files["File"];
- var form = Files;//定义接收类型的参数
-
- IList<Hashtable> fileResultList = new List<Hashtable>();
- IFormFileCollection cols = Request.Form.Files;
- if (cols == null || cols.Count == 0)
- {
- return new ApiResult(ReturnCode.GeneralError, "没有上传文件");
- }
- foreach (IFormFile file in cols)
- {
- //定义文件数组后缀格式
- string[] LimitFileType = { ".XLS", ".XLSX" , ".DOC", ".DOCX" , ".RTF" , ".AVI", ".RM", ".MP4", ".RMVB", ".WMV", ".MTV", ".MOV", ".MPG", ".MPEG", ".JPG", ".JPEG", ".GIF", ".PNG", ".BMP", ".PDF", ".GLB" };
- string[] LimitExcelFileType = { ".XLS", ".XLSX"};
- string[] LimitWordFileType = {".DOC", ".DOCX", ".RTF" };
- string[] LimitVideoFileType = { ".AVI", ".RM", ".MP4", ".RMVB", ".WMV", ".MTV", ".MOV", ".MPG", ".MPEG" };
- string[] LimitPicFileType = { ".JPG", ".JPEG", ".GIF", ".PNG", ".BMP" };
- string[] LimitPdfFileType = { ".PDF"};
- string[] Limit3DFileType = { ".GLB" };
- //LimitFileType.Union(LimitExcelFileType).ToArray().Union(LimitWordFileType).ToArray().Union(LimitVideoFileType).ToArray().Union(LimitPicFileType).ToArray();
- //获取文件后缀是否存在数组中
- string currentFileExtension = Path.GetExtension(file.FileName).ToUpper();
- if (LimitFileType.Contains(currentFileExtension))
- {
- //为了查看文件就不在重新生成文件名称了
- var dateDirectory = DateTime.Now.ToString("yyyyMM");
- var relativePath = Path.Combine("wwwroot/uploads/Files/", _claims.Linsence + "/" + dateDirectory);
- var directoryPath = Path.Combine(Directory.GetCurrentDirectory(), relativePath);
- var fileRelativePath = Path.Combine(relativePath + "/", file.FileName);
- if (!Directory.Exists(directoryPath))
- {
- Directory.CreateDirectory(directoryPath);
- }
- var path = Path.Combine(directoryPath + "/", file.FileName);
- using (var stream = new FileStream(path, FileMode.Create))
- {
- await Task.Run(()=>{
- //再把文件保存的文件夹中
- file.CopyTo(stream);
- Hashtable hash = new Hashtable();
- hash.Add("file", "/" + fileRelativePath);
- if (LimitExcelFileType.Contains(currentFileExtension))
- {
- hash.Add("typeValue", "1");
- }
- if (LimitWordFileType.Contains(currentFileExtension))
- {
- hash.Add("typeValue", "2");
- }
- if (LimitVideoFileType.Contains(currentFileExtension))
- {
- hash.Add("typeValue", "4");
- }
- if (LimitPicFileType.Contains(currentFileExtension))
- {
- hash.Add("typeValue", "3");
- }
- if (LimitPdfFileType.Contains(currentFileExtension))
- {
- hash.Add("typeValue", "5");
- }
- if (Limit3DFileType.Contains(currentFileExtension))
- {
- hash.Add("typeValue", "6");
- }
- hash.Add("name", file.Name);
- fileResultList.Add(hash);
- });
-
- }
- }
- else
- {
- return new ApiResult(ReturnCode.GeneralError, "请上传指定格式的文件");
- }
- }
- log.Info($"[结束] 上传文件信息.【{DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss:fff")}】");
- return new ApiResult<IList<Hashtable>>(fileResultList);
- }
- catch (Exception ex)
- {
- log.Info($"[异常] 上传文件信息.【{DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss:fff")}】");
- return new ApiResult(ReturnCode.GeneralError, ex.Message + ex.InnerException);
- }
- }
-
- }
- }
|