【问题标题】:"No file uploaded or URL provided" when calling ocr.space API调用 ocr.space API 时“未上传文件或提供 URL”
【发布时间】:2016-06-08 22:56:42
【问题描述】:

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

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

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" 中的 @ 字符是什么?

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

所以我不认为错误指向的文件不是预期的。 我宁愿怀疑我的代码中有语法错误。

【问题讨论】:

    标签: c# api curl


    【解决方案1】:

    错误信息告诉你出了什么问题:

    未上传文件或提供网址

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

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

    幸运的是,这很容易。 This question 演示了设置多部分请求的语法。

    这段代码对我有用:

    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();
    }
    

    【讨论】:

    • 这就像一个魅力。我从中学到了很多,谢谢 Nate。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-02-04
    • 2019-04-27
    • 1970-01-01
    • 2020-04-14
    • 1970-01-01
    • 2021-09-20
    • 2018-03-22
    相关资源
    最近更新 更多