C# 如何以可读格式将excel文件上载到azure blob?

C# 如何以可读格式将excel文件上载到azure blob?,c#,azure,azure-storage,azure-storage-blobs,C#,Azure,Azure Storage,Azure Storage Blobs,我正在将excel文件上载到azure存储容器。当文件上传后,我尝试从门户下载并打开它,打开失败,因为文件格式和扩展名不匹配。此外,与文件对应的“大小”列中没有大小。我看不出错误。代码在asp.net core 3.1中,带有c#。 这是我的密码 CloudStorageAccount cloudStorageAccount = CloudStorageAccount.Parse(connectionString); // to the azure account CloudBlobClient

我正在将excel文件上载到azure存储容器。当文件上传后,我尝试从门户下载并打开它,打开失败,因为文件格式和扩展名不匹配。此外,与文件对应的“大小”列中没有大小。我看不出错误。代码在asp.net core 3.1中,带有c#。 这是我的密码

CloudStorageAccount cloudStorageAccount = CloudStorageAccount.Parse(connectionString); // to the azure account
CloudBlobClient cloudBlobClient = cloudStorageAccount.CreateCloudBlobClient();
CloudBlobContainer cloudBlobContainer = cloudBlobClient.GetContainerReference(containerName); // container in which the file will be uploaded
blob = cloudBlobContainer.GetBlockBlobReference(f); // f is file name
await blob.UploadFromStreamAsync(s); // this is a memory stream
blob.Properties.ContentType = fileType;

根据我的测试,我们可以使用以下代码将excel文件上载到Azure blob存储

  • 安装SDK
  • 我的excel文件(.xlsx)

  • 代码

  • static async Task Main(字符串[]args)
    {
    var filepath=@“D:\test.xlsx”;
    var-storageAccount=CloudStorageAccount.Parse(“”);
    var cloudBlobClient=storageAccount.CreateCloudBlobClient();
    var cloudBlobContainer=cloudBlobClient.GetContainerReference(“test1”);
    var blob=cloudBlobContainer.GetBlockBlobReference(Path.GetFileName(filepath));
    blob.Properties.ContentType=Get(Path.GetFileName(filepath));
    使用(var stream=File.OpenRead(filepath))
    {
    等待blob.UploadFromStreamAsync(流);
    }
    //下载文件
    filepath=@“D:\test\”+blob.Name;
    使用(var stream=File.OpenWrite(filepath))
    {
    等待blob.DownloadToStreamAsync(流);
    }
    }
    //获取文件内容类型
    静态字符串获取(字符串文件名)
    {
    var provider=new FileExtensionContentTypeProvider();
    字符串内容类型;
    如果(!provider.TryGetContentType(文件名,out contentType))
    {
    contentType=“应用程序/八位字节流”;
    }
    返回contentType;
    }
    

    “此外,与文件对应的“大小”列中没有大小。”->在尝试上载之前,您是否返回到
    MemoryStream
    的开头?如果不是,则从
    MemoryStream
    的末尾上传(即在写入任何内容之后)。请编辑您的问题以包含
    s
    的生成。另外,请避免添加不必要的标记。这种情况不是ASP.NET Core独有的,尤其是ASP.NET Core 3.1或ASP.NET Core MVC。您可能没有以二进制格式上载,或者文件末尾缺少字节。使用类似Beyond Compare的工具将原始文件与最终文件进行比较。查看文件中间是否有错误,是否可以提供文件上传的所有代码?你有什么其他问题吗?如果你没有其他顾虑,可以吗?它可能会帮助更多的人。如果失败,为什么要将内容类型指定为八位字节流?我可以上传一个包含八位字节sream内容类型的Excel文件吗?
    Install-Package Microsoft.Azure.Storage.Blob -Version 11.1.1
    Install-Package Microsoft.AspNetCore.StaticFiles -Version 2.2.0
    
    static async Task Main(string[] args)
            {
                var filepath = @"D:\test.xlsx";
                var storageAccount = CloudStorageAccount.Parse("<connection string>");
                var cloudBlobClient = storageAccount.CreateCloudBlobClient();
                var cloudBlobContainer = cloudBlobClient.GetContainerReference("test1");
                var blob = cloudBlobContainer.GetBlockBlobReference(Path.GetFileName(filepath));
    
                blob.Properties.ContentType = Get(Path.GetFileName(filepath));
    
                using (var stream = File.OpenRead(filepath))
                {
    
                       await blob.UploadFromStreamAsync(stream);
    
    
                }
                //download file
                filepath = @"D:\test\" + blob.Name;
                using (var stream = File.OpenWrite(filepath))
                {
    
                    await blob.DownloadToStreamAsync(stream);
                }
    
    
            }
    
            // get the file content type
            static string Get(string fileName)
            {
                var provider = new FileExtensionContentTypeProvider();
                string contentType;
                if (!provider.TryGetContentType(fileName, out contentType))
                {
                    contentType = "application/octet-stream";
                }
                return contentType;
            }