简体   繁体   English

从Azure Blob存储中检索pdf

[英]Retrieve pdf from azure blob-storage

I'm trying to retrieve pdf documents from my azure blob-storage. 我正在尝试从我的azure blob存储中检索pdf文档。 I am getting the document, but when I try to open it, it tells me that it cant read the data. 我正在获取文档,但是当我尝试打开它时,它告诉我它无法读取数据。

[HttpGet("GetFromBlob")]
    public async Task<ActionResult> GetFileFromBlob(string id)
    {

        MemoryStream ms = new MemoryStream();

        CloudStorageAccount storageAccount = CloudStorageAccount.Parse("connectionString");

        CloudBlobClient BlobClient = storageAccount.CreateCloudBlobClient();
        CloudBlobContainer c1 = BlobClient.GetContainerReference("blobName");

        if (await c1.ExistsAsync())
        {
            CloudBlob file = c1.GetBlobReference("FileReference");

            if (await file.ExistsAsync())
            {
                await file.DownloadToStreamAsync(ms);
                Stream blobStream = file.OpenReadAsync().Result;
                return File(blobStream, file.Properties.ContentType, file.Name);
            }
            else
            {
                return Content("File does not exist");
            }
        }
        else
        {
            return Content("Container does not exist");
        }
    }

Turns out that my file format was wrong 原来我的文件格式不对

Please try use the code below, which works fine at my side: 请尝试使用下面的代码,该代码对我而言效果很好:

In the code block if(await file.ExistsAsync()) : 在代码块if(await file.ExistsAsync())

        if (await file.ExistsAsync())
        {
            await file.DownloadToStreamAsync(ms);
            var stream = await file.OpenReadAsync();
            return File(stream, "application/pdf");
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM