Anil D
Anil D

Reputation: 2009

Azure Blob files Download in MVC

I have created an application using "window azure cloud service" where i m uplodaing/downloading azure blob files like this

Upload Code

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");
    }

Download Code

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

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

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

Here is my view

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

Situation is, i m setting a Download limit for Users, so i need to get File size before downloading so that i can check if "Download Limit" is reached or not.

enter image description here

you can see here that there is a "Data Left" field. Before downloading another file i need to check if

    Downloaded File size > Data Left

then it should not download.

Please suggest

Upvotes: 1

Views: 968

Answers (1)

cillierscharl
cillierscharl

Reputation: 7117

You would have to check the size of the blob before deciding if there is enough bandwidth for the download.

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

Spoiler : this will actually query the blob, so I would assume a transaction charge every hit not to mention the increased latency of having to wait the request out; I would suggest you record the filesize on upload and save that off to your db or other persistent storage.

Upvotes: 1

Related Questions