Javascript 对来自客户端的输入图像使用Jimp
我试图用一个简单的输入文件从React中获取一个图像,并在Express服务器上用Jimp调整它的大小,然后用它做其他事情。问题是我无法用Jimp读取图像,因为它需要字符串、缓冲区或ArrayBuffer,而输入图像是文件类型。我已经尝试将文件转换为缓冲区,但没有成功。我怎样才能让Jimp毫无问题地阅读它Javascript 对来自客户端的输入图像使用Jimp,javascript,express,npm,jimp,Javascript,Express,Npm,Jimp,我试图用一个简单的输入文件从React中获取一个图像,并在Express服务器上用Jimp调整它的大小,然后用它做其他事情。问题是我无法用Jimp读取图像,因为它需要字符串、缓冲区或ArrayBuffer,而输入图像是文件类型。我已经尝试将文件转换为缓冲区,但没有成功。我怎样才能让Jimp毫无问题地阅读它 我将在这里写一个与问题相关的代码,中间有其他的东西,我确信不会妨碍JIMP,因为文件在调用Jimp之前是完整的。 反应 const onInputFile = (e) => { le
我将在这里写一个与问题相关的代码,中间有其他的东西,我确信不会妨碍JIMP,因为文件在调用Jimp之前是完整的。 反应
const onInputFile = (e) => {
let file = e.target.files[0];
const formData = new FormData();
formData.append("signature", signature.file, signature.file.fileName);
formData.append("id", props.art.id);
axios
.post("http://localhost:9000/createpdf", formData, {
headers: {
"Content-Type": "multipart/form-data",
authorization: props.userData.token,
},
})
.then(res => {
console.log(res);
})
.catch(err => {
console.log(err);
});
};
return(
<div>
<input
type="file"
id="inputFileID"
onChange={onInputFile}
name="inputFileName"
/>
</div>
);
app.post("/createpdf", async (req, res) => {
// I'm using formidable module to get the FormData from the request
// Just skipping that part, it works, I'll call the file "myImageFile"
res.status(200).json({ status: "ok" });
await Jimp.read(myImageFile) // Error in this line
.then(resultImg => {
resultImg.scale()
.getBufferAsync(Jimp.MIME_JPEG)
.then(finalImg => {
// Upload finalImg to Cloud
})
.catch(err => {
console.log(err);
});
})
.catch(err => {
console.log(err);
});
});