打开存储在 Azure Blob 存储中的 pdf 文件(使用字节)

问题描述 投票:0回答:4

我正在尝试调整旧逻辑以支持来自 blob 的文件,任何人都可以指导我如何打开存储在 azure blob 存储中的 pdf 文件。

我尝试搜索并找到了答案如何从Azure Blob存储将文件下载到浏览器 这是使用 SAS 配置来做到这一点(如果我没记错的话)。

有什么方法可以转换为字节吗?

从 Windows 位置打开 pdf 文件的早期逻辑

Response.Buffer = true;
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = "application/pdf";
Response.AppendHeader("content-disposition", "inline; filename=" + mapid + ".pdf");

FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read);
BinaryReader br = new BinaryReader(fs);
Byte[] dataBytes = br.ReadBytes((int)(fs.Length - 1));
Response.BinaryWrite(dataBytes);
br.Close();
fs.Close();

我正在重写从 blob 读取文件的逻辑,下面的代码是我迄今为止尝试过的,

Byte[] dataBytes1;
CloudBlockBlob blobfile = GetStorageAccount(true).GetBlockBlobReference(filename);
blobfile.FetchAttributes();

using (StreamReader blobfilestream = new StreamReader(blobfile.OpenRead()))
{
    dataBytes1 = blobfilestream.CurrentEncoding.GetBytes(blobfilestream.ReadToEnd());
}
Byte[] value = BitConverter.GetBytes(dataBytes1.Length - 1);
Response.BinaryWrite(value);

但是文件无法打开,并出现错误“加载失败”。 如果这是一个好的方法,谁能指导我?

c# azure azure-blob-storage
4个回答
0
投票

您可以使用 DownloadToStreamAsync 并使用

Response.OutputStream
作为目标流。

await blob.DownloadToStreamAsync(Response.OutputStream);

0
投票

您可以使用

DownloadToByteArray
,示例代码(在asp.net mvc项目中)如下,在我这边工作得很好:

        public ActionResult DownloadFile()
        {
            CloudStorageAccount storageAccount = new CloudStorageAccount(new StorageCredentials("your_account", "your_key"), true);
            CloudBlobClient client = storageAccount.CreateCloudBlobClient();
            CloudBlobContainer blobContainer = client.GetContainerReference("t11");
            CloudBlockBlob blob = blobContainer.GetBlockBlobReference("ss22.pdf");

            blob.FetchAttributes();

            long fileByteLength = blob.Properties.Length;
            byte[] fileContent = new byte[fileByteLength];
            for (int i=0;i<fileByteLength;i++)
            {
                fileContent[i] = 0x20;
            }

            blob.DownloadToByteArray(fileContent,0);

            Response.BinaryWrite(fileContent);

            return new EmptyResult();
        }

或者按照 @hardkoded 提到的,您可以根据需要使用

DownloadToStreamAsync
DownloadToStream

示例代码如下(asp.net mvc项目):

        public ActionResult DownloadFile()
        {
            CloudStorageAccount storageAccount = new CloudStorageAccount(new StorageCredentials("your_account", "your_key"),true);
            CloudBlobClient client = storageAccount.CreateCloudBlobClient();
            CloudBlobContainer blobContainer = client.GetContainerReference("t11");
            CloudBlockBlob blob = blobContainer.GetBlockBlobReference("ss22.pdf");

            blob.DownloadToStream(Response.OutputStream);

            return new EmptyResult();
        }

测试结果如下:


0
投票

正如上面硬编码提到的,您需要使用 DownloadToStreamAsync。下面是我的代码。

        blobfile.FetchAttributes();

        using (StreamReader blobfilestream = new StreamReader(blobfile.OpenRead()))
        {
            dataBytes1 = blobfilestream.CurrentEncoding.GetBytes(blobfilestream.ReadToEnd());
            await blobfile.DownloadToStreamAsync(Response.OutputStream);
        }

        Byte[] value = BitConverter.GetBytes(dataBytes1.Length - 1);
        string mimeType = "application/pdf";
        Response.AppendHeader("Content-Disposition", "inline; filename="+ filename);

        return File(value, mimeType);

0
投票
     public async System.Threading.Tasks.Task<ActionResult> DownloadFile()
    {
        CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
        ConfigurationManager.ConnectionStrings["azureconnectionstring"].ConnectionString);
        CloudBlobClient client = storageAccount.CreateCloudBlobClient();
        CloudBlobContainer blobContainer = client.GetContainerReference("my-blob-storage");
    CloudBlockBlob blob = blobContainer.GetBlockBlobReference("filename.pdf");
        var exists = blob.Exists(); // to verify if file exist
        blob.FetchAttributes();
        byte[] dataBytes1;
        using (StreamReader blobfilestream = new StreamReader(blob.OpenRead()))
        {
            dataBytes1 = blobfilestream.CurrentEncoding.GetBytes(blobfilestream.ReadToEnd());
            await blob.DownloadToStreamAsync(Response.OutputStream);
        }

        Byte[] value = BitConverter.GetBytes(dataBytes1.Length - 1);
        string mimeType = "application/pdf";
        Response.AppendHeader("Content-Disposition", "inline; filename=" + "filename.pdf");

        return File(value, mimeType);

}

© www.soinside.com 2019 - 2024. All rights reserved.