逻辑应用程序功能应用程序读写二进制文件

问题描述 投票:0回答:1

我遇到了一个奇怪的问题,我已经解决了好几天了。 我有下面的逻辑应用程序:

我的逻辑应用程序应该将 Excel(模板)作为来自 Sharepoint 的输入,写入它(使用函数应用程序),并将新的 Excel 文件输出到 SFTP 组件。 为了简单地描述我的问题,我要:

  1. 从 Sharepoint 读取 Excel 文件
  2. 将其放入 C# 函数应用程序中,顺便说一下,它应该输出相同的文件(因此无需进行 Excel 操作)
  3. 将输出文件写入 FTP。

当我在本地运行函数应用程序并使用 Postman 提交数据时,一切看起来都很好。我保存函数二进制输出,并且可以将其作为 Excel 文件打开。 但是,当我将函数应用程序部署到逻辑应用程序时,我可以将输出保存到 FTP,但生成的输出文件不是有效的 Excel 文件。 在逻辑应用程序 SFTP 组件中,我使用“@binary(body('ConvertJsonToExcelV2'))”

我做错了什么?请帮忙! 这是我的 C# Function 应用程序代码:

ConvertJsonToExcelV2.cs:

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Attributes;
using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Enums;
using Microsoft.Extensions.Logging;
using Microsoft.OpenApi.Models;
using NwcDevFuncApps.ConvertJsonToExcelHelper;
using System.Net;
using System.Threading.Tasks;

namespace NwcDevFuncApps
{
    public static class ConvertJsonToExcelV2
    {
        [FunctionName("ConvertJsonToExcelV2")]
        [OpenApiOperation(operationId: "ConvertJsonToExcelRun", tags: new[] { "convertjsontoexcel" })]
        [OpenApiSecurity("function_key", SecuritySchemeType.ApiKey, Name = "code", In = OpenApiSecurityLocationType.Query)]
        [OpenApiRequestBody(contentType: "application/json", bodyType: typeof(string), Description = "The root object of the body is jsontoexcel. An Excel can be included in the body as a template or pre-data (jsontoexcel.prependdata.$content)")]
        [OpenApiResponseWithBody(statusCode: HttpStatusCode.OK, contentType: "application/octet-stream", bodyType: typeof(object), Description = "The OK response")]
        public static async Task<IActionResult> Run(
        [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req, ILogger log)
        {
            ConvertJsonToExcelWorkerV2 w = new ConvertJsonToExcelWorkerV2(req, log);
            IActionResult result = await w.DoWork();
            return result;
        }
    }
}

ConvertJsonToExcelWorkerV2.cs:

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Runtime.ExceptionServices;
using System.Text;
using System.Threading.Tasks;

namespace NwcDevFuncApps.ConvertJsonToExcelHelper
{
    internal class ConvertJsonToExcelWorkerV2
    {
        private HttpRequest Req { get; set; } = null;
        private ILogger Log { get; set; } = null;

        public ConvertJsonToExcelWorkerV2(HttpRequest req, ILogger log)
        {
            Req = req;
            Log = log;
        }

        public async Task<IActionResult> DoWork()
        {
            FileContentResult res = null;
            JObject jsonData = null;

            string requestBody = new StreamReader(Req.Body).ReadToEnd();

            using (JsonTextReader reader = new JsonTextReader(new StringReader(requestBody)))
            {
                jsonData = (JObject)JToken.ReadFrom(reader);

                var prependJValue = (JValue)jsonData.SelectToken("jsontoexcel.prependdata.$content");
                var prependBase64String = prependJValue?.ToString();

                using (MemoryStream packageStream = new MemoryStream(Convert.FromBase64String(prependBase64String)))
                {
                    packageStream.Position = 0;

                    byte[] bytes = packageStream.ToArray();
                    res = new FileContentResult(bytes, "application/octet-stream");

                    packageStream.Close();
                }
                reader.Close();
            }
            return res;
        }
    }
}
azure-functions azure-logic-apps
1个回答
0
投票
  1. 将其放入 C# 函数应用程序中,顺便说一下,它应该输出相同的文件(因此无需进行 Excel 操作)

如果您发送的是从 Base64 数据转换而来的二进制文件,那么您需要提供如下内容:

{
"$content-type":@{body('Get_file_content_using_path')['$content-type']},
"$content":@{outputs('Compose')['$content']}
}

enter image description here

然后:

enter image description here

输出:

enter image description here

如果二进制格式正确,您将得到像我一样的输出。我也同意@Skin的观点,逻辑应用程序足以进行许多数据操作。

© www.soinside.com 2019 - 2024. All rights reserved.