今天来看看.net上传图片到服务器的方式
public class ControlPresetUploadInput { /// <summary> /// 通道编号 /// </summary> public string ChannelId { set; get; } /// <summary> /// 预置位编号 /// </summary> public int PresetId { set; get; } /// <summary> /// 传入的文件 /// </summary> public IFormFile InputImageFile { set; get; } }
[HttpPost] [Route("uploadImage")] [Authorize(IntelligentRemotePatrolPermissions.PresetInformations.Default)] public async Task<IResponseOutput> UploadImage([FromForm] ControlPresetUploadInput input) { string url = string.Empty; if (input.InputImageFile == null) return ResponseOutput.Ok(new PresetUploadMinioDto()); try { string name = $"{input.ChannelId + "-" + input.PresetId}.jpg"; url = await SubmitImagesToMinioAsync(input.InputImageFile, name); //存库记录 await _fileUploadInformation.InsertAsync(new FileUploadInformation { Length = input.InputImageFile.Length, Url = url, Type = input.InputImageFile.ContentType, Size = null, Name = name, Model = "Preset", }); } catch (Exception ex) { throw new UserFriendlyException(ex.Message); } return ResponseOutput.Ok(url); }
上传文件到服务器 (都差不多)
public class ExcelImportInput { /// <summary> /// 传入的文件 /// </summary> public IFormFile file { set; get; } } public class ExcelImportExtensionInput : ExcelImportInput { /// <summary> /// 各类ID,可以是单位ID,也可以是上级ID等等 /// </summary> public string id { get; set; } }
[HttpPost] [Route("importUnitsByXml")] public async Task<IActionResult> ImportUnitsByXml([FromForm] ExcelImportInput input) { if (input.file == null || input.file.Length <= 0) return Json(new { Success = false, Message = "请上传文件" }); if (!input.file.FileName.ToLower().Contains(".xml")) return Json(new { Success = false, Message = "请上传xml文件" }); string sWebRootFolder = _hostingEnvironment.ContentRootPath; string sFileName = $"units_{DateTime.Now:yyMMddHHmmssfff}.xml"; var fullPath = Path.Combine(sWebRootFolder, "wwwroot", "ImportXml", sFileName); FileHelper.CreateDirectoryIfFilePathNotExists(fullPath); try { FileInfo file = new FileInfo(fullPath); using (FileStream fs = new FileStream(file.ToString(), FileMode.Create)) { input.file.CopyTo(fs); fs.Flush(); } XmlDocument doc = new XmlDocument(); doc.Load(fullPath); var list = await _ledgerMessageAppService.GetXmlModelAttributes(new PatrolEquipmentDto(), doc.InnerXml); //剔除type不是单位的实体 <20>: = 区域巡视主机 <21>: = 边缘节点 list.RemoveAll(s => !",20,21,".Contains(s.type)); await _ledgerMessageAppService.XmlToAreaPatrolHost(list); return Json(new { Success = true, Message = "导入成功" }); } catch (Exception ex) { return Json(new { Success = false, Message = ex.Message }); } }