从Azure Blob

时间:2017-03-27 11:02:03

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

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

fileName是正确的,并提供上传文件的名称,但是,我不知道如何设置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();
        }

1 个答案:

答案 0 :(得分:2)

首先,正如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)
        };

    }

}