Save binary file to blob from httppostedfile

Viewed 3340

Can somebody provide code to save an uploaded file to azure blob in binary? I currently use a text save which is incredibly slow on huge files, reading/saving to blob, line by line.

Private Function ReadFile(ByVal file As HttpPostedFile) As String
        Dim result As String = ""
        Dim objReader As New System.IO.StreamReader(file.InputStream)
        Do While objReader.Peek() <> -1
            result = result & objReader.ReadLine() & vbNewLine
        Loop
        Return result
    End Function

Thanks

2 Answers

6 years later it seems Dennis Burton's answer isn't compatible with WindowsAzure.Storage v9.3.2.

For me this works:

IFormFile postedFile = null;
var contentType = postedFile.ContentType;
var blobName = postedFile.FileName;

var connectionString = "YOURSTORAGEACCOUNT_CONNECTIONSTRING";
var storageAccount = CloudStorageAccount.Parse(connectionString);
var blobClient = storageAccount.CreateCloudBlobClient();

var container = blobClient.GetContainerReference("YOURCONTAINERNAME");
await container.CreateIfNotExistsAsync();

var blob = container.GetBlockBlobReference(blobName);
blob.Properties.ContentType = contentType;
using (var streamContents = postedFile.OpenReadStream())
{
    await blob.UploadFromStreamAsync(streamContents);
}
Related