【发布时间】:2021-05-17 11:08:43
【问题描述】:
我正在使用 .Net 5 构建 Web API,我有一个包含图像的模型,我想用图像发布模型以将图像保存到文件系统中并将图像路径放入数据库。
假设我们有我们的模型:
public partial class Document
{
public int id { get; set; }
public string name { get; set; }
public string imageUrl { get; set; }
}
我试过了:
[Route("api/Images")]
public class ImageController : Controller
{
[HttpPost("upload")]
public async Task<IActionResult> Upload(IFormFile file)
{
try
{
var path = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot/images", file.FileName);
var stream = new FileStream(path, FileMode.Create);
file.CopyToAsync(stream);
return Ok(new { length = file.Length, name = file.FileName });
}
catch
{
return BadRequest();
}
}
}
我仍然需要将我的模型与图像文件一起传递。
【问题讨论】:
-
你read the docs了吗?
-
是的,我检查了表单只包含文件,而不是更多模型道具
-
html中的二进制数据只能发送到方式1)转换为base 64字符串2)作为Mime附件添加到消息中。请参阅:docs.microsoft.com/en-us/previous-versions/office/developer/…
标签: c# asp.net-core-webapi .net-5