简体   繁体   English

从Azure Blob存储下载文件

[英]Download File from Azure Blob Storage

I have an application that allows users to upload photos, which are stored in Azure Blob Storage. 我有一个允许用户上传照片的应用程序,这些照片存储在Azure Blob存储中。 The user also has the ability to view these photos. 用户还可以查看这些照片。 To view them, we want the application to download the image to the default download location. 要查看它们,我们希望应用程序将图像下载到默认下载位置。 At the moment, the upload works perfect. 目前,上传工作完美。 But the download function that I found for Azure API doesn't seem to be doing anything. 但是我为Azure API找到的下载功能似乎没有做任何事情。 Also, I can't really specify a download location because this functionality needs to work on desktop/laptops as well as on mobile devices which have different default directories. 此外,我无法真正指定下载位置,因为此功能需要在台式机/笔记本电脑以及具有不同默认目录的移动设备上运行。

This seems like it should be simple but I cannot find anything to help me. 这似乎应该很简单,但我找不到任何可以帮助我的东西。

Here is a sample of my code: 以下是我的代码示例:

CloudBlobContainer container = blobClient.GetContainerReference("photos");
CloudBlob blob = container.GetBlobReference(photo.BlobUrl);
//copy blob from cloud to local gallery
blob.DownloadToFile(photo.ImageName);

The blob.DownloadToFile(photo.ImageName); blob.DownloadToFile(photo.ImageName); causes a server request but nothing happens, aka no file is downloaded. 导致服务器请求但没有任何反应,也就是没有下载文件。

Any reason you cannot have the client download the file directly from blob storage and use the built-in browser capability to save it? 您是否有任何理由不能让客户端直接从blob存储中下载文件并使用内置的浏览器功能来保存它? If the files are private, it is pretty easy to generate a SAS signature. 如果文件是私有的,则生成SAS签名非常容易。 This will offload the download from coming through your server and remove that overhead. 这将卸载通过您的服务器下载并删除该开销。

Here: 这里:

blob.DownloadToFile(photo.ImageName);

You need to tell it where to save the file and the name you want it to be: 你需要告诉它保存文件的位置和你想要的名称:

blob.DownloadToFile("C:\mycutepuppy.jpg");

You could use System.Environment.SpecialFolder enum to figure out the folder based on the current system then append what you want the image name to be; 你可以使用System.Environment.SpecialFolder枚举来找出基于当前系统的文件夹,然后追加你想要的图像名称;

Here is a more complete example: 这是一个更完整的例子:

var imgPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), + photo.ImageName);

blob.DownloadToFile(imgPath);

This would save the image to the correct path where the user's desktop directory is. 这会将图像保存到用户桌面目录所在的正确路径。 Here is the documentation on that enum for special folders. 以下是特殊文件夹枚举的文档。

http://msdn.microsoft.com/en-us/library/system.environment.specialfolder.aspx http://msdn.microsoft.com/en-us/library/system.environment.specialfolder.aspx

I wonder what do you expect from this code, but especially in the case of different platform this would not work. 我想知道您对此代码的期望是什么,但特别是在不同平台的情况下,这不起作用。 I suggest that you use the DownloadToStream method instead. 我建议您使用DownloadToStream方法。 Then you can decide what to do with the stream. 然后,您可以决定如何处理流。 For example ask the user where does he want to save the file. 例如,询问用户他想要保存文件的位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM