【发布时间】: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