Logic App集成Function App读写二进制文件异常求助
问题:Logic App调用Function App后生成的Excel文件无效
流程背景
我的Logic App流程简化后为:
- 从SharePoint读取Excel文件
- 传入C# Function App(仅原样输出文件,不做Excel操作)
- 将输出文件写入FTP
本地运行Function App并用Postman测试时,保存的二进制输出可正常打开为Excel文件。但部署到Logic App后,虽能将输出保存至FTP,生成的文件却无法正常打开。在Logic App的SFTP组件中,我使用了表达式@binary(body('ConvertJsonToExcelV2'))。
相关代码
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; } } }
解决方案
问题核心是Logic App对Function返回数据的自动序列化处理,以及你使用的转换表达式重复编码导致文件损坏,具体修正步骤:
修正SFTP组件的表达式
将原来的@binary(body('ConvertJsonToExcelV2'))替换为:@body('ConvertJsonToExcelV2').$content因为Function返回的
FileContentResult在Logic App中会被自动封装为包含$content(Base64编码的文件内容)和$contentType的JSON对象,SFTP连接器可直接识别并解码该字段为二进制文件,无需额外转换。开启Function调用的二进制模式
在Logic App调用Function的动作设置中,开启"Binary mode"选项,避免Logic App对返回的二进制数据进行不必要的JSON序列化处理,保证数据传输的准确性。
按上述调整后,生成的Excel文件即可正常打开。
内容的提问来源于stack exchange,提问作者Leonard
相关产品推荐
相关产品推荐

