【发布时间】:2021-05-21 06:21:07
【问题描述】:
当我尝试使用 ASP.NET Web API 2 和 Entity Framework 将图像作为二进制文件添加到我的数据库中时。我收到此错误:
此资源不支持请求实体的媒体类型“multipart/form-data”
这是我尝试过的代码:
[HttpPost]
public IHttpActionResult ImageUpload(HttpPostedFileBase postedFile)
{
byte[] bytes;
using (BinaryReader br = new BinaryReader(postedFile.InputStream))
{
bytes = br.ReadBytes(postedFile.ContentLength);
}
SchoolContext context = new SchoolContext();
context.Images.Add(new Image
{
Name = Path.GetFileName(postedFile.FileName),
ContentType = postedFile.ContentType,
ImageBinary = bytes
});
context.SaveChanges();
return Ok(new { messages = "Image uploaded!" });
}
【问题讨论】:
标签: c# entity-framework asp.net-web-api2