public async Task <IActionResult> Upload(FileUploadDto dto) { ResposeResult result = new ResposeResult(); if (dto.Type == 0) { return(Ok(result)); } FileModel fileModel = new FileModel(); string filePath = string.Empty; string webRootPath = _webHostEnvironment.WebRootPath; //获取wwwroot文件夹 string fileDir = $"{webRootPath}/upload/{DateTime.Now.ToShortDateString()}"; //文件目录 E:/upload/2020-06-20/ try { if (!Directory.Exists(fileDir)) { Directory.CreateDirectory(fileDir); } if (dto.Type == 1) { IFormFile file = dto.File; string fileExt = Path.GetExtension(file.FileName); //文件扩展名,不含“.” if (string.IsNullOrEmpty(fileExt)) { result.Msg = "找不到文件扩展名"; return(Ok(result)); } string newFileName = System.Guid.NewGuid().ToString() + "." + fileExt; //随机生成新的文件名 filePath = $"{fileDir}/{newFileName}"; //文件路径 //通过文件流的方式保存文件 using (var stream = new FileStream(filePath, FileMode.Create)) { await file.CopyToAsync(stream); } } else if (dto.Type == 2) { Stream fromFile = await HttpClientHelp.Download(dto.Url, HttpMethod.Get); string fileExt = Path.GetExtension(dto.Url); //文件扩展名 if (string.IsNullOrEmpty(fileExt)) { fileExt = "jpg"; //result.Msg = "找不到文件扩展名"; //return Ok(result); } string newFileName = System.Guid.NewGuid().ToString() + fileExt; //随机生成新的文件名 //已追加的方式 写入文件流 filePath = $"{fileDir}/{newFileName}";//文件路径 FileHelp.CopyFile(fromFile, filePath, 1024 * 1024); } } catch (Exception e) { if (System.IO.File.Exists(filePath)) { System.IO.File.Delete(filePath); } result.Msg = e.Message; return(Ok(result)); } result.IsSuccess = true; result.Code = HttpStatusCode.Created; fileModel.AbsolutePath = filePath; fileModel.VirtualPath = filePath.Replace(webRootPath, ""); result.Data = fileModel; return(Ok(result)); }