Asp.net 从Azure Blob下载文件

Asp.net 从Azure Blob下载文件,asp.net,azure,model-view-controller,azure-storage,Asp.net,Azure,Model View Controller,Azure Storage,我正试图从Azure blob服务器下载一个文件,由于某种原因,它给了我错误参数类型“Edm.Int32”和“Edm.String”与此操作不兼容。在WHERE谓词附近,第1行,第84列。 文件名是正确的,并且给出了上传文件的名称,但是,我不确定如何设置OpenWrite来下载文件。如果我理解正确,OpenWrite将设置文件下载的位置。但是,我只需要能够点击“下载”按钮开始下载,并让文件开始下载,用户选择在默认情况下这样做,通常是“下载” 首先,正如haim770所说,错误似乎不是由Azure

我正试图从Azure blob服务器下载一个文件,由于某种原因,它给了我错误
参数类型“Edm.Int32”和“Edm.String”与此操作不兼容。在WHERE谓词附近,第1行,第84列。

文件名是正确的,并且给出了上传文件的名称,但是,我不确定如何设置OpenWrite来下载文件。如果我理解正确,OpenWrite将设置文件下载的位置。但是,我只需要能够点击“下载”按钮开始下载,并让文件开始下载,用户选择在默认情况下这样做,通常是“下载”


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

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

如果要使客户端能够通过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)
        };

    }

}
公共异步任务Get() { 尝试 { var-storageAccount=CloudStorageAccount.Parse(“{connection string}”); var blobClient=storageAccount.CreateCloudBlobClient(); var Blob=await blobClient.GetBlobReferenceFromServerAsync(新Uri(“https://{storageaccount}.Blob.core.windows.net/{mycontainer}/{blobname.txt}”); var isExist=await Blob.ExistsAsync(); 如果(!isExist){ return Request.CreateErrorResponse(HttpStatusCode.NotFound,“未找到文件”); } HttpResponseMessage=新的HttpResponseMessage(HttpStatusCode.OK); Stream blobStream=await Blob.OpenReadAsync(); message.Content=newstreamcontent(blobStream); message.Content.Headers.ContentLength=Blob.Properties.Length; message.Content.Headers.ContentType=新系统.Net.Http.Headers.MediaTypeHeaderValue(Blob.Properties.ContentType); message.Content.Headers.ContentDisposition=新系统.Net.Http.Headers.ContentDispositionHeaderValue(“附件”) { FileName=“{blobname.txt}”, 大小=Blob.Properties.Length }; 返回消息; } 捕获(例外情况除外) { 返回新的HttpResponseMessage { StatusCode=HttpStatusCode.InternalServerError, 内容=新的字符串内容(例如消息) }; } }
首先,正如haim770所说,错误似乎不是由Azure存储代码引起的,请调试代码以找到导致错误的代码段

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

如果要使客户端能够通过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)
        };

    }

}
公共异步任务Get() { 尝试 { var-storageAccount=CloudStorageAccount.Parse(“{connection string}”); var blobClient=storageAccount.CreateCloudBlobClient(); var Blob=await blobClient.GetBlobReferenceFromServerAsync(新Uri(“https://{storageaccount}.Blob.core.windows.net/{mycontainer}/{blobname.txt}”); var isExist=await Blob.ExistsAsync(); 如果(!isExist){ return Request.CreateErrorResponse(HttpStatusCode.NotFound,“未找到文件”); } HttpResponseMessage=新的HttpResponseMessage(HttpStatusCode.OK); Stream blobStream=await Blob.OpenReadAsync(); message.Content=newstreamcontent(blobStream); message.Content.Headers.ContentLength=Blob.Properties.Length; message.Content.Headers.ContentType=新系统.Net.Http.Headers.MediaTypeHeaderValue(Blob.Properties.ContentType); message.Content.Headers.ContentDisposition=新系统.Net.Http.Headers.ContentDispositionHeaderValue(“附件”) { FileName=“{blobname.txt}”, 大小=Blob.Properties.Length }; 返回消息; } 捕获(例外情况除外) { 返回新的HttpResponseMessage { StatusCode=HttpStatusCode.InternalServerError, 内容=新的字符串内容(例如消息) }; } }
听起来你的问题更多的是(文档)数据库查询,而不是Azure存储。@haim770我甚至没有想到这一点,但从逻辑上说,你可能是对的,我现在再看一件事:
blob.DownloadToStream(fileStream)
将实际在您的web服务器上而不是在客户端计算机上下载文件。@GauravMantri如何将其更改为下载到客户端计算机?@CaptainCanada^--我们不要在评论中询问其他问题-StackOverflow不支持“讨论”请另外问一个问题。听起来你的问题更多的是关于(文档)数据库查询,而不是Azure存储。@haim770我从来没有想过这一点,但从逻辑上说,你可能是对的,我现在再看一件事:
blob.DownloadToStream(fileStream)
将实际在您的web服务器上而不是在客户端计算机上下载文件。@GauravMantri如何将其更改为下载到客户端计算机?@CaptainCanada^--我们不要在评论中询问其他问题-StackOverflow不支持“讨论”请另外问一个问题。如果用户中止下载,如何确保流关闭?给定的代码是如何隐式处理的?如果用户中止下载,如何确保流关闭?给定的代码如何隐式处理这一点?