打开存储在Azure Blob存储中的pdf文件(使用字节) [英] Open a pdf file(using bytes) stored in Azure Blob storage
问题描述
我正在尝试调整旧的逻辑以支持blob中的文件,谁能指导我如何打开存储在azure blob存储器中的pdf文件.
I am trying to tweak the old logic to support files from blob, Can anyone guide me how to open a pdf file which is stored in azure blob storage.
我尝试搜索并找到了答案如何从Azure Blob存储将文件下载到浏览器 这是使用SAS配置做到的(如果我没记错的话).
I tried to search and found the answer How to download a file to browser from Azure Blob Storage which is using SAS configuration to do that (if i am not wrong).
通过转换为字节有什么办法吗?
Is there any way to do by converting to bytes?
从Windows位置打开pdf文件的早期逻辑
Earlier logic to open a pdf file from windows location
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读取文件的逻辑,下面的代码是到目前为止我尝试过的代码,
I am rewritting the logic to read the file from blob, below code is what i have tried so far,
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);
但是文件未打开,错误为无法加载". 如果这是个好方法,谁能指导我?
But the file is not opening with error "Failed to Load". Can anyone guide me if this is good approach to do it?
推荐答案
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);
}
这篇关于打开存储在Azure Blob存储中的pdf文件(使用字节)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!