【问题标题】:Azure Logic App Store Excel Email Attachment To Blob, Download and ReadAzure Logic App Store Excel 电子邮件附件到 Blob、下载和阅读
【发布时间】:2021-02-25 09:15:55
【问题描述】:

我设置了一个 Azure 逻辑应用程序,用于侦听具有特定标题的电子邮件。该电子邮件有一个 Excel 附件。当电子邮件到达时,它会将附件推送到 Azure Blob 并将消息放入队列中。

然后我有一个函数应用程序来侦听该消息并尝试处理该文件。

我有以下代码将 blob 作为流下载:

BlobContainerClient containerClient = _blobServiceClient.GetBlobContainerClient(containerName);
BlobClient blobClient = containerClient.GetBlobClient(fileName);
BlobDownloadInfo download = await blobClient.DownloadAsync();
using Stream downloadFileStream = new MemoryStream();
await download.Content.CopyToAsync(downloadFileStream);
downloadFileStream.Position = 0;

然后我将其转换为如下所示的 Azure blob 对象:

public class AzureBlob
{
    public string Id { get; set; }
    public string Name { get; set; }
    public string ContentBytes { get; set; }
    public string ContentType { get; set; }
    public int Size { get; set; }
    public bool IsInline { get; set; }
    public DateTime LastModifiedDateTime { get; set; }
    public string ContentId { get; set; }
}

使用此代码:

StreamReader reader = new StreamReader(downloadFileStream);
string fileContents = reader.ReadToEnd();
AzureBlob blob = JsonSerializer.Deserialize<AzureBlob>(fileContents);

然后我尝试像这样使用 OpenXML 打开它:

(“ContentType”返回为“application/vnd.openxmlformats-officedocument.spreadsheetml.sheet”)

Stream stockFileStream = new MemoryStream(Encoding.Unicode.GetBytes(blob.ContentBytes));
SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Open(stockFileStream, true);

但我得到的错误是:

System.IO.FileFormatException:文件包含损坏的数据。

谁能帮我找出我做错了什么吗?

【问题讨论】:

  • 您是否尝试过将此文件打开到任何处理此格式的应用程序中,以便您至少可以确定它是否真的损坏了?
  • 当我这样做时,excel 也无法打开它 await File.WriteAllBytesAsync(@"C:\Users\Trevo\Desktop\KockStock.xlsx", Encoding.Unicode.GetBytes(blob.ContentBytes ));
  • 能否确认azure logic app存储的excel文件完好无损?
  • @FrankGong 请问我该怎么做?
  • 因此,如果它没有在 Excel 中打开并且来自电子邮件,则认为它是有害的,因为它实际上可能是它所伪装的任何东西。您可以随时使用记事本查看内容。

标签: c# azure-blob-storage openxml


【解决方案1】:

我发现blob.ContentBytes是一个base64字符串,你需要使用Convert.FromBase64String将它转换成字节[]。

Stream stockFileStream = new MemoryStream(Convert.FromBase64String(blob.ContentBytes));

【讨论】:

    【解决方案2】:

    通过稍微改变类让它工作:

    public class AzureBlob
    {
        public string Id { get; set; }
        public string Name { get; set; }
        public byte[] ContentBytes { get; set; }
        public string ContentType { get; set; }
        public int Size { get; set; }
        public bool IsInline { get; set; }
        public DateTime LastModifiedDateTime { get; set; }
        public string ContentId { get; set; }
    }
    

    对我的代码稍作改动,将其转换为流:

    // get the file
    BlobContainerClient containerClient = _blobServiceClient.GetBlobContainerClient(containerName);
    BlobClient blobClient = containerClient.GetBlobClient(fileName);
    BlobDownloadInfo download = await blobClient.DownloadAsync();
            
    // convert the stream to an azure blob object
    StreamReader reader = new StreamReader(download.Content);
    string fileContents = reader.ReadToEnd();
    AzureBlob blob = JsonSerializer.Deserialize<AzureBlob>(fileContents);
    
    // convert the stock file to a stream
    Stream stockFileStream = new MemoryStream(blob.ContentBytes);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-24
      • 2021-03-06
      • 2017-09-15
      • 1970-01-01
      • 1970-01-01
      • 2014-05-10
      相关资源
      最近更新 更多