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

时间:2018-10-31 14:50:15

标签: c# azure azure-storage-blobs

我正在尝试调整旧的逻辑以支持blob中的文件,谁能指导我如何打开存储在azure blob存储中的pdf文件。

我尝试搜索并找到答案How to download a file to browser from Azure Blob Storage 这是使用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);

但是文件未打开,错误为“无法加载”。 如果这是个好方法,谁能指导我?

4 个答案:

答案 0 :(得分:0)

您可以使用DownloadToStreamAsync并将Response.OutputStream用作目标流。

await blob.DownloadToStreamAsync(Response.OutputStream);

答案 1 :(得分: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所述,您可以根据需要使用DownloadToStreamAsyncDownloadToStream

示例代码如下(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();
        }

测试结果如下: enter image description here

答案 2 :(得分: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);

答案 3 :(得分: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);

}