通过Ajax将图片发送到API

时间:2015-11-08 20:21:18

标签: javascript jquery ajax web-services

我试图调用此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仅接受四个参数:

  • apiKey:" helloworld" (免费许可证)
  • file:upload jpg image
  • url:远程图片的位置
  • 语言:" eng"英语来自defualt

我已经使用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;
&#13;
&#13;

2 个答案:

答案 0 :(得分:2)

<强> SOLUTION:

&#13;
&#13;
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;
&#13;
&#13;

答案 1 :(得分:0)

正如@charlietfl指出的那样,

formData.append("file", "@5.jpg");

不是文件,只是文件名。

文档说该文件必须是Multipart encoded image file with filename,因此我猜您必须在base64中对图像进行编码,然后再通过API发送。 开始there

它虽然有效cURL,但

curl --form "file=@5.jpg" [...]

实际上是指向真实的图像,但JS只会将其解释为字符串。