C# 从 Windows Azure 存储下载返回空文件,没有任何异常

标签 c# .net windows-store-apps azure-blob-storage azure-mobile-services

我正在尝试将 Windows Azure 存储用于我的带有移动服务的 Windows 应用商店应用程序来存储图像。我按照本指南进行了上传工作:

http://www.windowsazure.com/en-us/develop/mobile/tutorials/upload-images-to-storage-dotnet/

但是,我找不到任何有关下载文件的 Material 。我什至找不到 Windows 应用商店版本的类引用!如果有人可以指导我查看文档,我将不胜感激。

无论如何,我编写了代码,但它似乎不起作用:

public static async System.Threading.Tasks.Task DownloadUserImage(SQLUser userData)
{
    var usersFolder = await GetUsersFolder();
    var imageUri = new Uri(userData.ImageUri);
    var accountName = "<SNIP>";
    var key = "<SNIP>";

    StorageCredentials cred = new StorageCredentials(accountName, key);
    CloudBlobContainer container = new CloudBlobContainer(new Uri(string.Format("https://{0}/{1}", imageUri.Host, userData.ContainerName)), cred);
    CloudBlockBlob blob = container.GetBlockBlobReference(userData.ResourceName);

    var imageFile = await usersFolder.CreateFileAsync(userData.Id.ToString() + ".jpg", CreationCollisionOption.ReplaceExisting);
    using (var fileStream = await imageFile.OpenAsync(FileAccessMode.ReadWrite))
    {
        try
        {
            await blob.DownloadToStreamAsync(fileStream);
        }
        catch (Exception e)
        {
            Tools.HandleLiveException(e);
        }
    }
}

此代码会导致创建空文件,但不会引发任何异常。如果我将 imageUri 的值粘贴到浏览器,它就会开始下载文件并成功完成下载。但是,由于某种原因,我的程序没有。

请问有什么帮助吗?

最佳答案

显然,我打开流的方式是错误的。这是一个修复:

public static async System.Threading.Tasks.Task DownloadUserImage(SQLUser userData)
{
    var usersFolder = await GetUsersFolder();
    var imageUri = new Uri(userData.ImageUri);
    var accountName = "<SNIP>";
    var key = "<SNIP>";

    StorageCredentials cred = new StorageCredentials(accountName, key);
    CloudBlobClient client = new CloudBlobClient(new Uri(string.Format("https://{0}", imageUri.Host)), cred);
    CloudBlobContainer container = client.GetContainerReference(userData.ContainerName);
    var blob = await container.GetBlobReferenceFromServerAsync(userData.ResourceName);

    var imageFile = await usersFolder.CreateFileAsync(userData.Id.ToString() + ".jpg", CreationCollisionOption.ReplaceExisting);
    using (var fileStream = await imageFile.OpenStreamForWriteAsync())
    {
        try
        {
            await blob.DownloadToStreamAsync(fileStream.AsOutputStream());
        }
        catch (Exception e)
        {
            Tools.HandleLiveException(e);
        }
    }
}

现在一切正常。

关于C# 从 Windows Azure 存储下载返回空文件,没有任何异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15836598/

相关文章:

c# - 将 Collection<Derived> 转换为 Collection<Base>

c# - 向后播放动画( Storyboard)

c# - C#中如何在两个字符串中查找相似的单词

c# - Dapper SetTypeMap 破坏了 DapperExtension 的 ClassMapper

c# - Windows Phone 8.1 和 CurrentAppSimulator

xaml - Windows 应用商店应用布局差异

c# - 禁用designer.cs中的自动更改

c# - Visual C# 2010 速成版 : Specify default access modifier for new classes?

.net - 通过 Win 10 Oct 2018 更新,Windows 可以识别闰秒。 .NET 的 DateTime 现在也是吗?

c# - 在动画中使用时模糊的文本 block