123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220 |
- using Microsoft.AspNetCore.Http;
- using Microsoft.AspNetCore.Mvc;
- using Microsoft.Extensions.Logging;
- 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 ImageController : BaseController
- {
- private readonly ILogger<ImageController> _logger;
- private readonly IClaimsAccessor _claims;
- /// <summary>
- /// 构造函数
- /// </summary>
- /// <param name="logger"></param>
- /// <param name="claims"></param>
- public ImageController(ILogger<ImageController> logger, IClaimsAccessor claims)
- {
- _logger = logger;
- _claims = claims;
- }
- /// <summary>
- /// 上传图片
- /// </summary>
- /// <param name="Files"></param>
- /// <returns></returns>
- [HttpPost("UploadImageAsync")]
- public async Task<ApiResult> UploadImageAsync(IFormCollection Files)
- {
- _logger.LogInformation("[开始] 上传图片信息.");
- try
- {
- //var form = Request.Form;//直接从表单里面获取文件名不需要参数
- string dd = Files["File"];
- var form = Files;//定义接收类型的参数
- Hashtable hash = new Hashtable();
- IFormFileCollection cols = Request.Form.Files;
- if (cols == null || cols.Count == 0)
- {
- return new ApiResult(ReturnCode.GeneralError, "没有上传文件");
- }
- foreach (IFormFile file in cols)
- {
- //定义图片数组后缀格式
- string[] LimitPictureType = { ".JPG", ".JPEG", ".GIF", ".PNG", ".BMP" };
- //获取图片后缀是否存在数组中
- string currentPictureExtension = Path.GetExtension(file.FileName).ToUpper();
- if (LimitPictureType.Contains(currentPictureExtension))
- {
- //为了查看图片就不在重新生成文件名称了
- var dateDirectory = DateTime.Now.ToString("yyyyMM");
- var relativePath = Path.Combine("wwwroot/uploads/images/", _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);
- hash.Add("file", "/" + fileRelativePath);
- });
-
- }
- }
- else
- {
- return new ApiResult(ReturnCode.GeneralError, "请上传指定格式的图片");
- }
- }
- return new ApiResult<Hashtable>(new Hashtable(hash));
- }
- catch (Exception ex)
- {
- return new ApiResult(ReturnCode.GeneralError, ex.Message + ex.InnerException);
- }
- }
- /// <summary>
- /// 上传系统图片
- /// </summary>
- /// <param name="Files"></param>
- /// <returns></returns>
- [HttpPost("UploadSysImageAsync")]
- public async Task<ApiResult> UploadSysImageAsync(IFormCollection Files)
- {
- try
- {
- //var form = Request.Form;//直接从表单里面获取文件名不需要参数
- string dd = Files["File"];
- var form = Files;//定义接收类型的参数
- Hashtable hash = new Hashtable();
- IFormFileCollection cols = Request.Form.Files;
- if (cols == null || cols.Count == 0)
- {
- return new ApiResult(ReturnCode.GeneralError, "没有上传文件");
- }
- foreach (IFormFile file in cols)
- {
- //定义图片数组后缀格式
- string[] LimitPictureType = { ".JPG", ".JPEG", ".GIF", ".PNG", ".BMP" };
- //获取图片后缀是否存在数组中
- string currentPictureExtension = Path.GetExtension(file.FileName).ToUpper();
- if (LimitPictureType.Contains(currentPictureExtension))
- {
- //为了查看图片就不在重新生成文件名称了
- var dateDirectory = DateTime.Now.ToString("yyyyMM");
- var relativePath = Path.Combine("wwwroot/uploads/images/", _claims.Linsence + "/sys/" + 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);
- hash.Add("file", "/" + fileRelativePath);
- });
- }
- }
- else
- {
- return new ApiResult(ReturnCode.GeneralError, "请上传指定格式的图片");
- }
- }
- return new ApiResult<Hashtable>(new Hashtable(hash));
- }
- catch (Exception ex)
- {
- return new ApiResult(ReturnCode.GeneralError, ex.Message + ex.InnerException);
- }
- }
- /// <summary>
- /// 上传巡检系统图片
- /// </summary>
- /// <param name="Files"></param>
- /// <returns></returns>
- [HttpPost("UploadIspImageAsync")]
- public async Task<ApiResult> UploadIspImageAsync(IFormCollection Files)
- {
- try
- {
- //var form = Request.Form;//直接从表单里面获取文件名不需要参数
- string dd = Files["File"];
- var form = Files;//定义接收类型的参数
- Hashtable hash = new Hashtable();
- IFormFileCollection cols = Request.Form.Files;
- if (cols == null || cols.Count == 0)
- {
- return new ApiResult(ReturnCode.GeneralError, "没有上传文件");
- }
- foreach (IFormFile file in cols)
- {
- //定义图片数组后缀格式
- string[] LimitPictureType = { ".JPG", ".JPEG", ".GIF", ".PNG", ".BMP" };
- //获取图片后缀是否存在数组中
- string currentPictureExtension = Path.GetExtension(file.FileName).ToUpper();
- if (LimitPictureType.Contains(currentPictureExtension))
- {
- //为了查看图片就不在重新生成文件名称了
- var dateDirectory = DateTime.Now.ToString("yyyyMM");
- var relativePath = Path.Combine("wwwroot/uploads/images/", _claims.Linsence + "/isp/" + 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);
- hash.Add("file", "/" + fileRelativePath);
- });
- }
- }
- else
- {
- return new ApiResult(ReturnCode.GeneralError, "请上传指定格式的图片");
- }
- }
- return new ApiResult<Hashtable>(new Hashtable(hash));
- }
- catch (Exception ex)
- {
- return new ApiResult(ReturnCode.GeneralError, ex.Message + ex.InnerException);
- }
- }
- }
- }
|