简体   繁体   English

为什么我不能使用发布到Azure服务器的asp.net核心应用程序下载Azure Blob

[英]Why can't I download an Azure Blob using an asp.net core application published to Azure server

I am trying to download a Blob from an Azure storage account container. 我试图从Azure存储帐户容器下载Blob。 When I run the application locally, I get the correct "Download" folder C:\\Users\\xxxx\\Downloads. 当我在本地运行应用程序时,我得到正确的“下载”文件夹C:\\ Users \\ xxxx \\ Downloads。 When I publish the application to Azure and try to download the file, I get an error. 当我将应用程序发布到Azure并尝试下载该文件时,我收到错误。 I have tried various "Knownfolders", and some return empty strings, others return the folders on the Azure server. 我尝试了各种“Knownfolders”,有些返回空字符串,其他人返回Azure服务器上的文件夹。 I am able to upload files fine, list the files in a container, but am struggling with downloading a file. 我能够正常上传文件,列出容器中的文件,但我正在努力下载文件。

string conn = 
configuration.GetValue<string>"AppSettings:AzureContainerConn");
CloudStorageAccount storageAcct = CloudStorageAccount.Parse(conn);
CloudBlobClient blobClient = storageAcct.CreateCloudBlobClient();
CloudBlobContainer container = 
blobClient.GetContainerReference(containerName);

 Uri uriObj = new Uri(uri);

string filename = Path.GetFileName(uriObj.LocalPath);

// get block blob reference  
CloudBlockBlob blockBlob = container.GetBlockBlobReference(filename);

Stream blobStream = await blockBlob.OpenReadAsync();

string _filepath = _knownfolder.Path + "\\projectfiles\\";
Directory.CreateDirectory(_filepath);

_filepath = _filepath + filename;
Stream _file = new MemoryStream();
try
{
 _file = File.Open(_filepath, FileMode.Create, FileAccess.Write);

 await blobStream.CopyToAsync(_file);
 }
finally
{
  _file.Dispose();
}

The expected end result is the file ends up in the folder within the users "Downloads" folder. 预期的最终结果是文件最终位于用户“Downloads”文件夹中的文件夹中。

Since you're talking about publishing to Azure, the code is probably from a web application, right? 既然您正在谈论发布到Azure,那么代码可能来自Web应用程序,对吧? And the code for the web application runs on the server . 并且Web应用程序的代码在服务器上运行 Which means the code is trying to download the blob to the server running the web application . 这意味着代码正在尝试将blob下载到运行Web应用程序的服务器

To present a downloadlink to the user to enable them to download the file, use the FileStreamResult which 要向用户提供下载链接以使其能够下载文件,请使用FileStreamResult

Represents an ActionResult that when executed will write a file from a stream to the response. 表示ActionResult,执行时将文件从流写入响应。

A (pseudo code) example: 一个(伪代码)示例:

[HttpGet]
public FileStreamResult GetFile()
{
  var stream = new MemoryStream();
  CloudBlockBlob blockBlob = container.GetBlockBlobReference(filename);
  blockBlob.DownloadToStream(stream);
  blockBlob.Seek(0, SeekOrigin.Begin);
  return new FileStreamResult(stream, new MediaTypeHeaderValue("text/plain"))
  {
    FileDownloadName = "someFile.txt"
  };
}

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

相关问题 在asp.net中使用javascript在Azure BLOB存储上载期间,如何压缩视频? - How can i compress video during upload on Azure BLOB storage using javascript in asp.net? 使用ASP.NET Core 2.1将静态文件发布到Azure后无法加载 - Unable to load static file once it has been published to Azure, using ASP.NET Core 2.1 ASP.NET Core 应用程序日志未写入 Azure 应用服务中的 Blob - ASP.NET Core Application Logs Not Written To Blob in Azure App Service 从Azure Blob链接下载图像,但我只需要观看(Asp.NET Core) - Link from Azure Blob downloads image, but I need to just watch (Asp.NET Core) 使用ASP.NET中的File Uploader将图像上传到Azure Blob? - Uploading Image to Azure Blob using File Uploader in ASP.NET? 使用asp.net身份更新Azure Blob存储中的图像 - Updating Image In Azure Blob Storage using asp.net Identity 使用ASP.NET在Windows Azure blob存储上设置CORS - Set CORS on Windows Azure blob storage using ASP.NET 为什么我不能将我的ASP.Net项目从VS2015发布到Azure - Why can't I publish my ASP.Net project from VS2015 to Azure 为什么我无法在asp.net上的服务器上找到要下载的文件? - Why i can not find file on the server for download in asp.net? 我可以使用IP地址浏览到Azure VM上的ASP.NET应用程序吗? - Can I browse to my ASP.NET application on an Azure VM using an IP address?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM