using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Threading.Tasks; using Bowin.Common.Files; using Bowin.Common.JSON; using Bowin.Common.Utility; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; namespace YLShipBuildLandMap.Web.Controllers.SystemSetting { [Route("api/systemsetting/[controller]/[action]")] [ApiController] public class UploadController : ControllerBase { [HttpPost] public async Task Post(IFormFile imgFile, string dir) { string[] fileExts; string fileDescription; try { switch (dir) { case "image": fileExts = new string[] { ".jpg", ".jpeg", ".png", ".bmp", ".gif" }; fileDescription = "图片文件"; break; case "flash": fileExts = new string[] { ".swf" }; fileDescription = "flash文件"; break; case "media": fileExts = new string[] { ".wmv", ".avi", ".mp4", ".mp3" }; fileDescription = "媒体文件"; break; case "file": fileExts = new string[] { ".rar", ".doc", ".docx", ".xls", ".xlsx", ".zip", ".pdf", ".txt" }; fileDescription = "文档"; break; default: throw new Exception("不支持的上传文件类型。"); } var ext = Path.GetExtension(imgFile.FileName); if (!fileExts.Contains(ext)) { return new { error = 1, message = "只允许上传" + fileDescription + ",文件扩展名需要以" + string.Join("、", fileExts) + "结尾。" }.ToJson(); } string url = await FileHelper.Upload(imgFile); return new { error = 0, url }.ToJson(); } catch (Exception ex) { return new { error = 1, message = ex.Message }.ToJson(); } } } }