如何从ASP.NET Core中的application/JSON主体中提取包含Base64和文件扩展名的JSON对象



我将一个JSON对象从React传递到ASP.NET Core,该对象包含2个密钥,如下所示

formData={图像:";Base64String Here";,类型:";JPEG";}

url,
method: "POST",
data: {image: formData.image, type: formData.type},
headers: {
'Content-Type': 'application/json'
}
})

然后它被收集在我的控制器中,如下

public async Task<ActionResult<IEnumerable<AppImage>>> UploadImage()
{
string jsonString;
using (StreamReader reader = new StreamReader(Request.Body, Encoding.UTF8))
{
jsonString = await reader.ReadToEndAsync();
}
try
{
await using var transaction = await _context.Database.BeginTransactionAsync();
SQLAppImage sqlData = new SQLAppImage(_context);
ImageHelper ih = new ImageHelper(_configuration);
//String filePath = ih.UploadImage(image, type);
//AppImage appImage = new AppImage
//{
//    ImagePath = filePath,
//    FileType = type
//};
//sqlData.Add(appImage);
await transaction.CommitAsync();
return Ok();

}
catch (Exception e)
{
throw e;
}
}

但是我似乎无法通过JSON对象,你有什么建议可以帮助我吗?

提前感谢

要做到这一点,请创建一个如下所示的类:

public class UploadPhotoDto
{
public string Image { get; set; }
public string Type { get; set; }
}

并将其用作您的方法输入,如下图所示:

[HttpPost]
public async Task<ActionResult<IEnumerable<AppImage>>>UploadImage(UploadPhotoDto uploadPhotoDto)
{
//And you can access them
string base64 = uploadPhotoDto.Image;
string type = uploadPhotoDto.Type;
//Your Code 
}