之前写过一个往Microsoft Azure Storage Explorer里存储的功能,现在又要把东西给下载下来。
记录一下:
public string DownFileFromAzure()
{
StorageCredentials storageCredentials = new StorageCredentials(System.Configuration.ConfigurationManager.AppSettings["Blob_AccountName"].ToString(), System.Configuration.ConfigurationManager.AppSettings["Blob_AccountKey"].ToString());
CloudStorageAccount storageAccount = new CloudStorageAccount(storageCredentials, "core.chinacloudapi.cn", true);
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer blobContainer = blobClient.GetContainerReference("acsh");//容器 CloudBlockBlob blockBlobs = blobContainer.GetBlockBlobReference("2019/7/1/131JS0E5201907000005");//除了外层的容器外的全路径
string end= blockBlobs.DownloadText();
byte[] ensbyte = new byte[];
blockBlobs.DownloadToByteArray(ensbyte, );
blockBlobs.DownloadToFile(@"E:\DownFromAzure\Open", FileMode.OpenOrCreate);//直接下载到本地的文件
//blockBlobs.Delete(); return "";
}
2.然后,现在要把这个从Azure Blob中下载的文件以流的形式去转成pdf的样子下载出来。
MemoryStream stream = new MemoryStream();//声明一个流文件,用于接收DownloadToStream();
stream.Seek(,SeekOrigin.Begin);
blockBlobs.DownloadToStream(stream);
byte[] b = stream.ToArray();
//string s = System.Text.Encoding.UTF8.GetString(b, 0, b.Length);
//byte[] fileByte = Convert.FromBase64String(s);
return b;
//blockBlobs.Delete();
、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、
SECURITY eCURITY = new SECURITY();
byte[] fileByte = eCURITY.DownFileFromAzure();
Response.Clear();
Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}.pdf","Test"));
Response.BinaryWrite(fileByte);
Response.ContentType="application/pdf";
Response.Flush();
Response.End();
return View();
在这里一开始,就是遇到超时的情况:其实这个超时是要重写ReadTimeOut的方法,MemoryStream是继承Stream类的。
但是这个length不为0,所以这个是从云上把文件给获取下来了。
因为在前端获取是将byte文件转成流文件的。所以,我在上面的获取流文件的时候,直接将
byte[] b = stream.ToArray();
然后用前端接收下,就把pdf的文件给下载下来l。