我试图调用此web service来识别图片中的文本(OCR),但是当使用jQuery中提供的示例时,调用始终会失败并出现此错误
System.InvalidOperationException:未上传文件或提供网址↵
bei OCRReaderMVCWeb.Controllers.OCRAPIController.Parse()in d:\ 3bitbucket \ 1ocr.aaaa.com \ OCRReaderMVCWeb \ \控制器OCRAPIController.cs:Zeile 243。
文档:https://ocr.a9t9.com/OCRAPI API仅接受四个参数:
我已经使用curl命令尝试了API,并且使用下面的代码段中注释的url工作正常。我知道你不能在没有iframe的情况下使用jQuery提交文档,因此提供的示例不正确。
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script>
//Prepare form data
var formData = new FormData();
formData.append("file", "@5.jpg");
//formData.append("url", "http://i.telegraph.co.uk/multimedia/archive/02007/Plate-1_2007614b.jpg"); //url of remote image
formData.append("language", "eng");
formData.append("apikey", "helloworld");
//Send OCR Parsing request asynchronously
jQuery.ajax({
url: "https://ocr.a9t9.com/api/Parse/Image",
data: formData,
dataType: 'json',
cache: false,
contentType: false,
processData: false,
type: 'Post',
success: function (ocrParsedResult) {
//Get the parsed results, exit code and error message and details
var parsedResults = ocrParsedResult["ParsedResults"];
var ocrExitCode = ocrParsedResult["OCRExitCode"];
var isErroredOnProcessing = ocrParsedResult["IsErroredOnProcessing"];
var errorMessage = ocrParsedResult["ErrorMessage"];
var errorDetails = ocrParsedResult["ErrorDetails"];
//If we have got parsed results, then loop over the results to do something
console.log("success? " );
if (parsedResults != null) {
//Uncomment these lines if parsing multiple results
$.each(parsedResults, function (index, value) {
var exitCode = value["FileParseExitCode"];
var parsedText = value["ParsedText"];
var errorMessage = value["ParsedTextFileName"];
var errorDetails = value["ErrorDetails"];
console.log("text: " + parsedText);
var pageText = '';
switch (+exitCode) {
case 1:
pageText = parsedText;
break;
case 0:
case -10:
case -20:
case -30:
case -99:
default:
pageText += "Error: " + errorMessage;
break;
}
});
}
}
});
</script>
&#13;
答案 0 :(得分:2)
<强> SOLUTION:强>
denyAccessUnlessGranted()
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script type="text/javascript">
function uploadPicture()
{
//Prepare form data
var formData = new FormData();
formData.append("file", $("#inputFileToLoad")[0].files[0]);
//formData.append("url", "http://i.telegraph.co.uk/multimedia/archive/02007/Plate-1_2007614b.jpg"); //url of remote image
formData.append("language", "eng");
formData.append("apikey", "helloworld");
callService(formData);
}
function callService(formData)
{
//Send OCR Parsing request asynchronously
jQuery.ajax({
url: "https://ocr.a9t9.com/api/Parse/Image",
data: formData,
dataType: 'json',
cache: false,
contentType: false,
processData: false,
type: 'POST',
success: function (ocrParsedResult) {
//Get the parsed results, exit code and error message and details
console.log("Success!");
var parsedResults = ocrParsedResult["ParsedResults"];
var ocrExitCode = ocrParsedResult["OCRExitCode"];
var isErroredOnProcessing = ocrParsedResult["IsErroredOnProcessing"];
var errorMessage = ocrParsedResult["ErrorMessage"];
var errorDetails = ocrParsedResult["ErrorDetails"];
//If we have got parsed results, then loop over the results to do something
if (parsedResults != null) {
//Uncomment these lines if parsing multiple results
$.each(parsedResults, function (index, value) {
var exitCode = value["FileParseExitCode"];
var parsedText = value["ParsedText"];
var errorMessage = value["ParsedTextFileName"];
var errorDetails = value["ErrorDetails"];
console.log("OCR: " + parsedText);
$("#text").html( parsedText);
});
}
}
});
}
</script>
&#13;
答案 1 :(得分:0)