“未上传文件或提供 URL"调用 ocr.space API 时 [英] "No file uploaded or URL provided" when calling ocr.space API

查看:39
本文介绍了“未上传文件或提供 URL"调用 ocr.space API 时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从我的 C# 应用程序调用此 API:https://ocr.space/OCRAPI

I'm trying to call this API from my C# app: https://ocr.space/OCRAPI

当我从 curl 调用它时,它工作正常:

When I call it from curl, it just works fine:

curl -k --form "file=@filename.jpg" --form "apikey=helloworld" --form "language=eng" https://api.ocr.space/Parse/Image

我是这样实现的:

 [TestMethod]
    public async Task  Test_Curl_Call()
    {

        var client = new HttpClient();

        String cur_dir = Directory.GetCurrentDirectory();

        // Create the HttpContent for the form to be posted.
        var requestContent = new FormUrlEncodedContent(new[] {
              new KeyValuePair<string, string>(  "file", "@filename.jpg"), //I also tried "filename.jpg"
               new KeyValuePair<string, string>(     "apikey", "helloworld" ),
        new KeyValuePair<string, string>( "language", "eng")});

        // Get the response.
        HttpResponseMessage response = await client.PostAsync(
           "https://api.ocr.space/Parse/Image",
            requestContent);

        // Get the response content.
        HttpContent responseContent = response.Content;

        // Get the stream of the content.
        using (var reader = new StreamReader(await responseContent.ReadAsStreamAsync()))
        {
            // Write the output.
            String result = await reader.ReadToEndAsync();
            Console.WriteLine(result);
        }

    }

我得到了这个答案:

{
    "ParsedResults":null,
    "OCRExitCode":99,
    "IsErroredOnProcessing":true,
    "ErrorMessage":"No file uploaded or URL provided",
    "ErrorDetails":"",
    "ProcessingTimeInMilliseconds":"0"
}

有什么线索吗?

"file=@filename.jpg" 中的@ 字符是什么?

What's the @ character for in "file=@filename.jpg"?

我将我的 filename.jpg 文件放在项目测试项目的 bin/debug 目录中,并在调试模式下运行我的测试项目.

I put my filename.jpg file in the project and test project bin/debug directory and run my test project in debug mode.

所以我不认为错误指向文件不在预期的地方.我宁愿怀疑我的代码中存在语法错误.

So I don't think the error points to the file not being where expected. I'd rather suspect a syntax error in my code.

推荐答案

错误消息告诉您出了什么问题:

The error message is telling you what's wrong:

未上传文件或提供网址

您在代码中向服务发送了一个文件名,但这与给 curl 一个文件名不同.curl 足够聪明,可以读取文件并根据您的请求上传内容,但是在您的 C# 代码中,您必须自己完成.步骤将是:

You sent a filename to the service in your code, but that's not the same thing as giving curl a filename. curl is smart enough to read the file and upload the contents with your request, but in your C# code, you'll have to do that yourself. The steps will be:

  1. 从磁盘读取文件字节.
  2. 创建一个包含两部分的多部分请求:API 密钥(helloworld")和文件字节.
  3. 将此请求发布到 API.

幸运的是,这很容易.这个问题演示了设置多部分的语法请求.

Fortunately, it's pretty easy. This question demonstrates the syntax to set up a multipart request.

此代码对我有用:

public async Task<string> TestOcrAsync(string filePath)
{
    // Read the file bytes
    var fileBytes = File.ReadAllBytes(filePath);
    var fileName = Path.GetFileName(filePath);

    // Set up the multipart request
    var requestContent = new MultipartFormDataContent();

    // Add the demo API key ("helloworld")
    requestContent.Add(new StringContent("helloworld"), "apikey");

    // Add the file content
    var imageContent = new ByteArrayContent(fileBytes);
    imageContent.Headers.ContentType = new MediaTypeHeaderValue("image/jpeg");
    requestContent.Add(imageContent, "file", fileName); 

    // POST to the API
    var client = new HttpClient();
    var response = await client.PostAsync("https://api.ocr.space/parse/image", requestContent);

    return await response.Content.ReadAsStringAsync();
}

这篇关于“未上传文件或提供 URL"调用 ocr.space API 时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆