Как загрузить файл Excel в azure blob в читаемом формате? - PullRequest
0 голосов
/ 09 января 2020

Я загружаю файл Excel в azure контейнер для хранения. Когда файл загружен, и я пытаюсь загрузить его обратно с портала и открыть его, открыть не удается, потому что формат файла и расширение не совпадают. Кроме того, нет размера в столбце размера, соответствующего файлу. Я не могу определить ошибку. Код в asp. net ядро ​​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;

1 Ответ

0 голосов
/ 10 января 2020

Согласно моему тесту, мы можем использовать следующий код для загрузки файла Excel в Azure хранилище BLOB-объектов

  1. Установить SDK
Install-Package Microsoft.Azure.Storage.Blob -Version 11.1.1
Install-Package Microsoft.AspNetCore.StaticFiles -Version 2.2.0

Мой файл Excel (.xlsx) enter image description here

Код

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;
        }

enter image description here enter image description here

...