【发布时间】: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