更新时间:2021-09-09 21:35:27
我通常只在 Mvc 控制器 中使用 HttpPostedFileBase 参数.在处理 ApiControllers 时,请尝试检查传入文件的 HttpContext.Current.Request.Files 属性:
I normally use the HttpPostedFileBase parameter only in Mvc Controllers. When dealing with ApiControllers try checking the HttpContext.Current.Request.Files property for incoming files instead:
[HttpPost]
public string UploadFile()
{
var file = HttpContext.Current.Request.Files.Count > 0 ?
HttpContext.Current.Request.Files[0] : null;
if (file != null && file.ContentLength > 0)
{
var fileName = Path.GetFileName(file.FileName);
var path = Path.Combine(
HttpContext.Current.Server.MapPath("~/uploads"),
fileName
);
file.SaveAs(path);
}
return file != null ? "/uploads/" + file.FileName : null;
}