【问题标题】:Azure Blob files Download in MVC在 MVC 中下载 Azure Blob 文件
【发布时间】:2013-09-13 05:23:55
【问题描述】:

我使用“window azure cloud service”创建了一个应用程序,我在其中上传/下载像这样的 azure blob 文件

上传代码

public ActionResult UploadImage_post(HttpPostedFileBase fileBase)
    {
        if (fileBase.ContentLength > 0)
        {
            Microsoft.WindowsAzure.StorageClient.CloudBlobContainer blobContainer =
                _myBlobStorageService.GetCloudBlobContainer();

            Microsoft.WindowsAzure.StorageClient.CloudBlob blob =
                blobContainer.GetBlobReference(fileBase.FileName);

            blob.UploadFromStream(fileBase.InputStream);
        }
        return RedirectToAction("UploadImage");
    }

下载代码

Microsoft.WindowsAzure.StorageClient.CloudBlobContainer blobContainer =
         _myBlobStorageService.GetCloudBlobContainer();

        Microsoft.WindowsAzure.StorageClient.CloudBlob blob =
            blobContainer.GetBlobReference(filename);

return Redirect(blobContainer.GetBlobReference(filename).Uri.AbsoluteUri);

这是我的看法

@foreach (var item in Model)
{
        <img src="@item" width="200" height="100" />
        <a href="/Blob/Download?filename=@item">Download</a> 
}

情况是,我正在为用户设置下载限制,所以我需要在下载之前获取文件大小,以便我可以检查是否达到“下载限制”。

您可以在此处看到有一个“Data Left”字段。在下载另一个文件之前,我需要检查是否

    Downloaded File size > Data Left

那么它不应该下载。

请推荐

【问题讨论】:

    标签: asp.net-mvc-3 azure


    【解决方案1】:

    在决定是否有足够的带宽供下载之前,您必须检查 blob 的大小。

    Microsoft.WindowsAzure.StorageClient.CloudBlob blob =
                blobContainer.GetBlobReference(filename);
    
    blob.FetchAttributes();
    
    var blobsize = blob.Properties.Length; // this is in bytes
    // is size less than bandwidth left. etc
    

    剧透:这实际上会查询 blob,所以我假设每次点击都会收取交易费用,更不用说不得不等待请求结束的延迟增加了;我建议您在上传时记录文件大小并将其保存到您的数据库或其他持久存储中。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-10-08
    • 2017-08-20
    • 2021-01-04
    • 2013-09-07
    • 1970-01-01
    • 1970-01-01
    • 2021-06-17
    相关资源
    最近更新 更多