我在我的网站上使用免费的 OCR.space api,并使用 base64Image 上传图像(从 html 文件中输入的仅 jpg 文件接收),并收到此错误:
{"OCRExitCode":99,"IsErroredOnProcessing":true,"ErrorMessage":["Unable to recognize the file type","E216:Unable to detect the file extension, or the file extension is incorrect, and no 'file type' provided in request. Please provide a file with a proper content type or extension, or provide a file type in the request to manually set the file extension."],"ProcessingTimeInMilliseconds":"0"}
我的代码是:
async function OCR(jpg) {
const url = 'https://api.ocr.space/parse/image';
const data = {
apikey: 'SECRET',
base64Image: jpg,
};
const options = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(data),
};
try {
const response = await fetch(url, options);
const json = await response.json();
return json;
} catch (error) {
console.error(error);
return { error: true };
}
}
而我的base64编码函数是:
function toBase64(file) {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onload = (e) => resolve(e.target.result);
reader.onerror = reject;
reader.readAsDataURL(file);
});
}
所以我跑步:
await OCR(await toBase64(/*HTML INPUT HERE*/))
我明白错误是什么,但我似乎找不到有关如何添加文件类型的文档,我该怎么做?
如果我没有完全弄错的话,docs描述了一个可以接受
filetype
的String
属性。有效的属性值为“PDF、GIF、PNG、JPG、TIF、BMP”。
根据该信息,我建议将您当前的代码更改为
const data = {
apikey: 'SECRET',
base64Image: jpg,
filetype: 'JPG' // <-- added this line
};