public async Task <IHttpActionResult> UploadEditorImageAsync() { object data = null; string tempDir = HttpContext.Current.Server.MapPath("~/temp"); if (!Directory.Exists(tempDir)) { Directory.CreateDirectory(tempDir); } string dateStr = DateTime.Now.ToString("yyyy-MM-dd"); string dstDir = HttpContext.Current.Server.MapPath("~/upload/file/editor/image/") + dateStr + "/"; if (!Directory.Exists(dstDir)) { Directory.CreateDirectory(dstDir); } var fileProvider = new MultipartFormDataStreamProvider(tempDir); var multipart = await Request.Content.ReadAsMultipartAsync(fileProvider); try { ValidatorProvider.ThrowIfEqual(multipart.FileData.Count, 0, "请选择上传的文件"); var fileData = multipart.FileData[0]; var fileName = fileData.Headers.ContentDisposition.FileName.Replace("\"", "").Replace("\"", ""); var ext = Path.GetExtension(fileName).ToLower(); string[] picExts = new string[] { ".jpg", ".jpeg", ".png", ".gif", ".bmp" }; ValidatorProvider.ThrowIfNotIn(ext, picExts, "上传的图片格式不正确"); var fi = new FileInfo(fileData.LocalFileName); ValidatorProvider.ThrowIfMoreThan((int)fi.Length, 5 * 1024 * 1024, "上传的图片不能大于5M"); string file = Guid.NewGuid().ToString("N") + ext; File.Move(fileData.LocalFileName, Path.Combine(dstDir, file)); var uri = Request.RequestUri; data = new { url = $"/upload/file/editor/image/{dateStr}/{file}", fullurl = $"{WebConfigs.UrlPrefix}upload/file/editor/image/{dateStr}/{file}" }; } catch (Exception e) { throw e; } finally { Request.RemoveTempFile(multipart.FileData); } return(Json(JsonApiResult.Ok(data))); }