c# - 在 Azure 函数中读取包含 pdf 的请求正文时出错

标签 c# http blob azure-functions

我有一个带有 blob 触发器的 azure 函数,我想在逻辑应用程序中使用它。然后我意识到逻辑应用程序只支持 HTTP 触发器 azure 函数。

我将在 blob 触发函数中运行良好的代码迁移到新的 http 触发 azure 函数。我将 blob 触发函数中收到的流替换为 http 触发函数中的请求主体,但我无法打开使用该流创建的 pdf。

Blob触发函数

public static void Run([BlobTrigger("attachments/{name}", Connection = "")]Stream blobPdf, string name, ILogger log)
{
    // Create MemoryStream
    var streamPdf = new MemoryStream();
    CopyStream(blobPdf, streamPdf);
    // Create PDF from MemoryStream
    var pdf = PdfReader.Open(streamPdf);
}

HTTP触发函数

public static async Task<IActionResult> Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req, ILogger log)
{
        // Create MemoryStream
        var streamPdf = new MemoryStream();
        CopyStream(req.Body, streamPdf);
        // Create PDF from MemoryStream
        var pdf = PdfReader.Open(streamPdf);
}

当它试图在最后一行打开 pdf 时弹出错误。 根据 pdf 文件,有 2 个错误。第一个是:

System.Private.CoreLib: Exception while executing function: ExtractTextFromPDF. PdfSharp: Invalid PDF file: no trailer found.

第二个是:

System.Private.CoreLib: Exception while executing function: ExtractTextFromPDF. PdfSharp: Unexpected token 'n' in PDF stream. The file may be corrupted. If you think this is a bug in PDFsharp, please send us your PDF file.

最佳答案

问题是在 Postman 中提交 pdf 的方式。 Binary 而不是 form-data 解决了这个问题。

关于c# - 在 Azure 函数中读取包含 pdf 的请求正文时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58338799/

相关文章:

c# - 为什么允许从父类(super class)到子类的隐式转换?

database - 来自关系数据库的类似 Couchdb 的 http 访问?

matlab - 通过 MatLab 计算 Blob 数量

c# - ComboBox 项目为空但数据源已满

c# - 我可以使用显式运算符来创建派生类吗?

c++ - 纯粹通过原始套接字连接连接到网站

json - Laravel "Malformed UTF-8 characters, possibly incorrectly encoded",如何修复?

javascript - 使用 Filesaver.js 保存 base64 图像

c# - 2 对单个 C# 类的 XAML 引用(通用应用程序 8.1)

java - 以编程方式在没有浏览器的情况下保持 HTTP session Activity