[英]How to upload an image to the ASP.NET CORE WEB API in BYTE ARRAY format using React JS, 400 bad request
We are trying to send some data to the ASP.NET CORE WEB API server and upload that data to a website using axios post request in React and the main problem is in sending image in BYTE ARRAY format to a server.我们正在尝试将一些数据发送到 ASP.NET CORE WEB API 服务器,并使用 React 中的 axios post 请求将该数据上传到网站,主要问题是将 BYTE ARRAY 格式的图像发送到服务器。 Server accepts images only in BYTE ARRAY format.服务器仅接受 BYTE ARRAY 格式的图像。 It gives us an error POST 400. Upload.jsx:47 Error: Request failed with status code 400. Please help.它给我们一个错误 POST 400。Upload.jsx:47 错误:请求失败,状态码为 400。请帮忙。 Here is the our code using react hooks:这是我们使用 react hooks 的代码:
import React, { useState } from "react";
import axios from "axios";
const Upload = () => {
const [upload, setUpload] = useState({
ruName: "",
azName: "",
kaName: "",
enName: "",
image: null,
shopId: localStorage.getItem("shopId")
});
const changeHandler = e => {
setUpload({ [e.target.name]: e.target.value });
};
const uploadHandler = async e => {
const base64 = await convertBase64(e.target.files[0]);
setUpload({ image: base64 });
};
const convertBase64 = file => {
return new Promise((resolve, reject) => {
const fileReader = new FileReader();
fileReader.readAsDataURL(file);
fileReader.onload = () => {
resolve(fileReader.result);
};
fileReader.onerror = error => {
reject(error);
};
});
};
const onAddCategory = async e => {
e.preventDefault();
try {
const res = await axios.post(
"https://midamoapi.ge/api/productcategories/",
upload
);
console.log(res);
} catch (err) {
console.log(err);
}
};
console.log(upload);
return (
<div className="container-add">
<input name="ruName" onChange={changeHandler} type="text" />
<input name="azName" onChange={changeHandler} type="text" />
<input name="enName" onChange={changeHandler} type="text" />
<input name="kaName" onChange={changeHandler} type="text" />
<input name="image" type="file" onChange={uploadHandler} />
<button className="btn" onClick={onAddCategory}>
Submit
</button>
</div>
);
};
export default Upload;
Request failed with status code 400请求失败,状态码 400
In your code, we can find that you use FileReader.readAsDataURL()
to get a base64 encoded string of the selected image, then post the data to your action method.在您的代码中,我们可以发现您使用FileReader.readAsDataURL()
获取所选图像的 base64 编码字符串,然后将数据发布到您的操作方法。
Please check if your action method and accepted parameter look like below.请检查您的操作方法和接受的参数是否如下所示。
Category class类别类
public class Category
{
public string RuName { get; set; }
public string AzName { get; set; }
public string EnName { get; set; }
public string KaName { get; set; }
public string Image { get; set; } //please note that Image property is string type
public int ShopId { get; set; }
}
Action method动作方法
[HttpPost]
public IActionResult AddCategory(Category category)
{
//...
//your code logic here
Test Result测试结果
Besides, if you'd like to process or save the image file using IFormFile
, you can try to pass the data via FormData
rather than in json format data through request body.此外,如果您想使用IFormFile
处理或保存图像文件,您可以尝试通过FormData
传递数据,而不是通过请求正文传递 json 格式的数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.