从 Azure 存储文件流式传输 blob
Filestream a blob from Azure storage
我在 Azure 中有图像,需要使用 pdfJet 添加到 pdf 中。
这是我在读取磁盘上的图像时使用的代码,但是我有很多图像,从 Azure 下载它们没有意义。
Image image = new Image(objects, new BufferedStream(new FileStream(LocalPath + "image.PNG", FileMode.Open, FileAccess.Read)), ImageType.PNG);
PS:这是在 asp.net 网络表单中完成的。
感谢您的帮助。
我现在正在使用以下功能阅读 PDF:
public MemoryStream DownloadToMemoryStream(DTO.BlobUpload b)
{
CloudStorageAccount storageAccount = Conn.SNString(b.OrgID);
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference(b.Container);
CloudBlockBlob blob = container.GetBlockBlobReference(b.FileName);
var sasToken = blob.GetSharedAccessSignature(new SharedAccessBlobPolicy()
{
Permissions = SharedAccessBlobPermissions.Read,
SharedAccessExpiryTime = DateTime.UtcNow.AddMinutes(10),//assuming the blob can be downloaded in 10 miinutes
}, new SharedAccessBlobHeaders()
{
ContentDisposition = "attachment; filename=file-name"
});
using (MemoryStream ms = new MemoryStream())
{
blob.DownloadToStream(ms);
return ms;
}
}
并且在我使用的 aspx 页面中:
MemoryStream pdfScript = B.DownloadToMemoryStream(b);
读取流:
SortedDictionary<Int32, PDFobj> objects = pdfFinalScript.Read(pdfScript);
但是我收到错误消息:
无法访问已关闭的流
我已经查看了如何打开流,但还没有成功。
能不能帮帮忙,谢谢
根据您的描述,您将从 Azure 下载 blob。
这里有几种方法可以参考。
创建一个具有读取权限的共享访问签名,然后 Content-Disposition header 设置和创建 blob URL 并使用它 URL。在这种情况下,blob 内容将直接从存储流式传输到客户端浏览器。
2.Get blob 和 DownloadFileFromBlob.
3.Download 文件到 exactly path in local.
网络表格:
您可以使用 Response.Redirect(blobUrl);
重定向 blob url 并下载它。
在 .aspx 中:
<asp:Button ID="Button1" runat="server" Text="Click Me" OnClick="Button1_Click" />
在aspx.cs中:
protected void Button1_Click(object sender, EventArgs e)
{
CloudStorageAccount account = new CloudStorageAccount(new StorageCredentials("accountname", "accountkey"), true);
var blobClient = account.CreateCloudBlobClient();
var container = blobClient.GetContainerReference("container");
var blob = container.GetBlockBlobReference("text.PNG");
var sasToken = blob.GetSharedAccessSignature(new SharedAccessBlobPolicy()
{
Permissions = SharedAccessBlobPermissions.Read,
SharedAccessExpiryTime = DateTime.UtcNow.AddMinutes(10),//assuming the blob can be downloaded in 10 miinutes
}, new SharedAccessBlobHeaders()
{
ContentDisposition = "attachment; filename=file-name"
});
using (MemoryStream ms = new MemoryStream())
{
blob.DownloadToStream(ms);
Image image = new Image(objects, ms, ImageType.PNG);
}
}
我在 Azure 中有图像,需要使用 pdfJet 添加到 pdf 中。
这是我在读取磁盘上的图像时使用的代码,但是我有很多图像,从 Azure 下载它们没有意义。
Image image = new Image(objects, new BufferedStream(new FileStream(LocalPath + "image.PNG", FileMode.Open, FileAccess.Read)), ImageType.PNG);
PS:这是在 asp.net 网络表单中完成的。
感谢您的帮助。
我现在正在使用以下功能阅读 PDF:
public MemoryStream DownloadToMemoryStream(DTO.BlobUpload b)
{
CloudStorageAccount storageAccount = Conn.SNString(b.OrgID);
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference(b.Container);
CloudBlockBlob blob = container.GetBlockBlobReference(b.FileName);
var sasToken = blob.GetSharedAccessSignature(new SharedAccessBlobPolicy()
{
Permissions = SharedAccessBlobPermissions.Read,
SharedAccessExpiryTime = DateTime.UtcNow.AddMinutes(10),//assuming the blob can be downloaded in 10 miinutes
}, new SharedAccessBlobHeaders()
{
ContentDisposition = "attachment; filename=file-name"
});
using (MemoryStream ms = new MemoryStream())
{
blob.DownloadToStream(ms);
return ms;
}
}
并且在我使用的 aspx 页面中:
MemoryStream pdfScript = B.DownloadToMemoryStream(b);
读取流:
SortedDictionary<Int32, PDFobj> objects = pdfFinalScript.Read(pdfScript);
但是我收到错误消息: 无法访问已关闭的流
我已经查看了如何打开流,但还没有成功。
能不能帮帮忙,谢谢
根据您的描述,您将从 Azure 下载 blob。 这里有几种方法可以参考。
创建一个具有读取权限的共享访问签名,然后 Content-Disposition header 设置和创建 blob URL 并使用它 URL。在这种情况下,blob 内容将直接从存储流式传输到客户端浏览器。
2.Get blob 和 DownloadFileFromBlob.
3.Download 文件到 exactly path in local.
网络表格:
您可以使用 Response.Redirect(blobUrl);
重定向 blob url 并下载它。
在 .aspx 中:
<asp:Button ID="Button1" runat="server" Text="Click Me" OnClick="Button1_Click" />
在aspx.cs中:
protected void Button1_Click(object sender, EventArgs e)
{
CloudStorageAccount account = new CloudStorageAccount(new StorageCredentials("accountname", "accountkey"), true);
var blobClient = account.CreateCloudBlobClient();
var container = blobClient.GetContainerReference("container");
var blob = container.GetBlockBlobReference("text.PNG");
var sasToken = blob.GetSharedAccessSignature(new SharedAccessBlobPolicy()
{
Permissions = SharedAccessBlobPermissions.Read,
SharedAccessExpiryTime = DateTime.UtcNow.AddMinutes(10),//assuming the blob can be downloaded in 10 miinutes
}, new SharedAccessBlobHeaders()
{
ContentDisposition = "attachment; filename=file-name"
});
using (MemoryStream ms = new MemoryStream())
{
blob.DownloadToStream(ms);
Image image = new Image(objects, ms, ImageType.PNG);
}
}