public async Task <IActionResult> UploadImage() { try { var file = Request.Form.Files[0]; string folderName = "/Uploads/Images"; string webRootPath = Path.Combine(Directory.GetCurrentDirectory(), "Uploads"); string newPath = Path.Combine(webRootPath, "Images"); if (!ImageHandler.CheckImageType(file.OpenReadStream())) { return(BadRequest("文件格式不正确")); } if (!Directory.Exists(newPath)) { Directory.CreateDirectory(newPath); } if (file.Length > 0) { string fileName = ImageHandler.GetRandomFileName(Path.GetExtension(ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"'))); string fullPath = Path.Combine(newPath, fileName); using (var stream = new FileStream(fullPath, FileMode.Create)) { await file.CopyToAsync(stream); } return(Ok($"{folderName}/{fileName}")); } return(BadRequest("上传失败")); } catch (Exception ex) { return(BadRequest("上传失败:" + ex.Message)); } }