我正在傳遞一個 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 物件,是否有任何建議可以幫助我
先感謝您
uj5u.com熱心網友回復:
為此,請創建一個類,如下所示:
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
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/457078.html
