C# Azure Logic应用商店Excel电子邮件附件到Blob,下载并阅读

C# Azure Logic应用商店Excel电子邮件附件到Blob,下载并阅读,c#,azure-storage-blobs,openxml,C#,Azure Storage Blobs,Openxml,我已经安装了一个azure logic应用程序,用于侦听带有特定标题的电子邮件。那封电子邮件有一个excel附件。当电子邮件到达时,它将附件推送到Azure blob,并将消息放入队列 然后,我有一个功能应用程序,可以监听该消息并尝试处理该文件 我有以下代码将blob作为流下载: BlobContainerClient containerClient = _blobServiceClient.GetBlobContainerClient(containerName); BlobClient bl

我已经安装了一个azure logic应用程序,用于侦听带有特定标题的电子邮件。那封电子邮件有一个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;
// 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);
然后我将其转换为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);
但我得到的错误是:

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


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

我找到了
blob。ContentBytes
是base64字符串,您需要使用它将其转换为bytes[]

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

通过稍微更改类使其正常工作:

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; }
}
对我的代码稍加修改,将其转换为流:

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;
// 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);
//获取文件
BlobContainerClient containerClient=\u blobServiceClient.GetBlobContainerClient(containerName);
BlobClient BlobClient=containerClient.GetBlobClient(文件名);
BlobDownloadInfo下载=等待blobClient.DownloadAsync();
//将流转换为azure blob对象
StreamReader=新的StreamReader(download.Content);
string fileContents=reader.ReadToEnd();
AzureBlob blob=JsonSerializer.Deserialize(文件内容);
//将股票文件转换为流
Stream stockFileStream=新内存流(blob.ContentBytes);

您是否尝试将此文件打开到任何处理此格式的应用程序中,以便您至少可以判断它是否真的损坏?当我这样做时,excel无法打开它,也无法等待文件。WriteAllBytes同步(@“C:\Users\Trevo\Desktop\KockStock.xlsx”,Encoding.Unicode.GetBytes(blob.ContentBytes));你能确认在代码中存储在Excel中的Excel文件是否是完整的Azure Apple < /代码>?@富兰克林我会怎么做?如果它在Excel中不打开,它来自电子邮件,认为它是有害的,因为它可以是字面上的任何东西,而不是它所假装的。您可以随时使用记事本检查内容。