【问题标题】:Download file from Azure Blob从 Azure Blob 下载文件
【发布时间】:2017-08-20 01:16:31
【问题描述】:

我正在尝试从我的 Azure blob 服务器下载一个文件,由于某种原因它给了我错误 The argument types 'Edm.Int32' and 'Edm.String' are incompatible for this operation. Near WHERE predicate, line 1, column 84.

文件名是正确的,并给出了上传文件的名称,但是,我不确定如何设置 OpenWrite 来下载文件。如果我理解正确,OpenWrite 会设置文件的下载位置。但是,我只需要能够单击“下载”按钮开始此操作,并在用户默认选择的任何地方开始下载文件,通常是“下载”

public EmptyResult Download(string id)
        {
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
            CloudConfigurationManager.GetSetting("filestorageideagen_AzureStorageConnectionString"));

            CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

            CloudBlobContainer container = blobClient.GetContainerReference("documentuploader");

            DocumentUps documentUps = db.DocumentUps.Find(id);

            string fileName = id.ToString() + documentUps.RevisionId.ToString() + documentUps.Attachment.ToString();

            CloudBlockBlob blob = container.GetBlockBlobReference(fileName);


            using (var fileStream = System.IO.File.OpenWrite(????))
            {
                blob.DownloadToStream(fileStream);
            }


            return new EmptyResult();
        }

【问题讨论】:

  • 听起来您的问题更像是(文档)数据库查询而不是 Azure 存储。
  • @haim770 我从来没想过,但从逻辑上思考,你可能是对的,我现在就看看它
  • 还有一件事:blob.DownloadToStream(fileStream); 实际上会将文件下载到您的 Web 服务器上,而不是客户端计算机上。
  • @GauravMantri 如何将其更改为下载到客户端计算机?
  • @CaptainCanada ^-- 我们不要在 cmets 中问其他问题 - StackOverflow 不支持“讨论”。请为此提出一个单独的问题。

标签: asp.net azure model-view-controller azure-storage


【解决方案1】:

首先,正如haim770所说,错误似乎不是Azure存储代码引起的,请调试代码找到导致错误的代码sn-p。

如何将其更改为下载到客户端计算机?

如果您想让客户端通过您的 Web API 下载 Azure Blob 存储,请参考以下代码来实现您的控制器操作。

public async Task<HttpResponseMessage> Get()
{
    try
    {
        var storageAccount = CloudStorageAccount.Parse("{connection string}");
        var blobClient = storageAccount.CreateCloudBlobClient();

        var Blob = await blobClient.GetBlobReferenceFromServerAsync(new Uri("https://{storageaccount}.blob.core.windows.net/{mycontainer}/{blobname.txt}"));
        var isExist = await Blob.ExistsAsync();

        if (!isExist) { 
            return Request.CreateErrorResponse(HttpStatusCode.NotFound, "file not found");
        }

        HttpResponseMessage message = new HttpResponseMessage(HttpStatusCode.OK);
        Stream blobStream = await Blob.OpenReadAsync();

        message.Content = new StreamContent(blobStream);
        message.Content.Headers.ContentLength = Blob.Properties.Length;
        message.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(Blob.Properties.ContentType);
        message.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment")
        {
            FileName = "{blobname.txt}",
            Size = Blob.Properties.Length
        };

        return message;

}    
catch (Exception ex)

    {

        return new HttpResponseMessage

        {
            StatusCode = HttpStatusCode.InternalServerError,
            Content = new StringContent(ex.Message)
        };

    }

}

【讨论】:

  • 如果用户中止下载,如何确保流关闭?给定代码是如何隐式处理的?
猜你喜欢
  • 2021-01-04
  • 1970-01-01
  • 2021-12-08
  • 2018-09-27
  • 2018-04-13
  • 2015-06-20
相关资源
最近更新 更多