问题
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.
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?
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();
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?
回答1:
You could use DownloadToStreamAsync and use the Response.OutputStream
as the destination stream.
await blob.DownloadToStreamAsync(Response.OutputStream);
回答2:
You can use DownloadToByteArray
, sample code(in asp.net mvc project) as below, and works fine at my side:
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();
}
Or as per @hardkoded mentioned, you can either use DownloadToStreamAsync
or DownloadToStream
per your need.
Sample code as below(asp.net mvc project):
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();
}
The test result as below:
回答3:
As hardkoded mentioned above, you need to use DownloadToStreamAsync. Below is my code.
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);
回答4:
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);
}
来源:https://stackoverflow.com/questions/53086162/open-a-pdf-fileusing-bytes-stored-in-azure-blob-storage