【问题标题】:Download file in azure storage using ASP.Net core2使用 ASP.Net core2 在 azure 存储中下载文件
【发布时间】:2018-09-10 21:44:12
【问题描述】:

我有 Web 应用程序,用户应该能够下载存储在 azure 存储中的文件。

public class FileDownloader
{
    private string constring = "myconnectionstring";

    public async Task download(string fileName, string directoryName)
    {
        try
        {
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(constring);
            CloudFileClient fileClient = storageAccount.CreateCloudFileClient();
            CloudFileShare share = fileClient.GetShareReference("myreference");
            string userName = Environment.UserName;


            if (await share.ExistsAsync())
            {
                // Get a reference to the root directory for the share.
                CloudFileDirectory rootDir = share.GetRootDirectoryReference();

                // Get a reference to the directory we created previously.
                CloudFileDirectory sampleDir = rootDir.GetDirectoryReference(directoryName);

                // Ensure that the directory exists.
                if (await sampleDir.ExistsAsync())
                {
                    // Get a reference to the file we created previously.
                    CloudFile file = sampleDir.GetFileReference(fileName);

                    // Ensure that the file exists.
                    if (await file.ExistsAsync())
                    {

                        await file.DownloadToFileAsync(string.Format("C:/Users/" + userName + "/Downloads/{0}", fileName), FileMode.Create);
                    }
                }
            }

        }
        catch (Exception ex)
        {

        }
    }
}

这是我的代码。

当我在本地环境(localhost)中运行应用程序时,它可以完美运行,但是当我部署它并将其托管在 azure 中时,它不会下载文件。

【问题讨论】:

  • 它不是在下载文件对我们一点帮助都没有。它以什么方式失败?您收到错误消息吗?如果是这样,它们是什么?你试过调试吗?你的代码在某处失败了吗?
  • 另外,如果你在 Azure 中托管这个,你认为C:/Users...... 会指向哪里?
  • 获取客户端文件下载路径的正确方法是什么
  • 您想在客户端机器上下载文件吗?您的代码实际上会在运行代码的服务器上下载文件(前提是您指定的下载路径正确)。
  • 是的。我想将文件下载到客户端的机器上。当我在 azure 上托管我的应用程序时,我应该怎么做

标签: c# azure download azure-storage asp.net-core-2.0


【解决方案1】:

DownloadToFileAsync() :它允许您下载应用程序目录中的文件,而不是客户端。

可以使用流下载吗?

How to download files from Azure Blob Storage with a Download Link

【讨论】:

    猜你喜欢
    • 2023-03-31
    • 1970-01-01
    • 2021-07-15
    • 2022-12-24
    • 2018-11-07
    • 1970-01-01
    • 2021-06-17
    • 2015-06-20
    • 2021-12-08
    相关资源
    最近更新 更多