之前写过一个往Microsoft Azure Storage Explorer里存储的功能,现在又要把东西给下载下来。
记录一下:
1 public string DownFileFromAzure() 2 { 3 StorageCredentials storageCredentials = new StorageCredentials(System.Configuration.ConfigurationManager.AppSettings["Blob_AccountName"].ToString(), System.Configuration.ConfigurationManager.AppSettings["Blob_AccountKey"].ToString()); 4 CloudStorageAccount storageAccount = new CloudStorageAccount(storageCredentials, "core.chinacloudapi.cn", true); 5 CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient(); 6 CloudBlobContainer blobContainer = blobClient.GetContainerReference("acsh");//容器 7 8 CloudBlockBlob blockBlobs = blobContainer.GetBlockBlobReference("2019/7/1/131JS0E5201907000005");//除了外层的容器外的全路径 9 string end= blockBlobs.DownloadText(); 10 byte[] ensbyte = new byte[100000000]; 11 blockBlobs.DownloadToByteArray(ensbyte, 0); 12 blockBlobs.DownloadToFile(@"E:\DownFromAzure\Open", FileMode.OpenOrCreate);//直接下载到本地的文件 13 //blockBlobs.Delete(); 14 15 return ""; 16 }2.然后,现在要把这个从Azure Blob中下载的文件以流的形式去转成pdf的样子下载出来。
MemoryStream stream = new MemoryStream();//声明一个流文件,用于接收DownloadToStream(); stream.Seek(0,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。
转载于:https://www.cnblogs.com/ZkbFighting/p/11122257.html
相关资源:数据结构—成绩单生成器