Dustine Tolete
Dustine Tolete

Reputation: 471

asp.net - Upload to Windows Azure with FileUpload

I've got this code from here on uploading blob files on Windows Azure with ASP.Net:

// Retrieve storage account from connection string.
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
    CloudConfigurationManager.GetSetting("StorageConnectionString"));

// Create the blob client.
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

// Retrieve reference to a previously created container.
CloudBlobContainer container = blobClient.GetContainerReference("mycontainer");

// Retrieve reference to a blob named "myblob".
CloudBlockBlob blockBlob = container.GetBlockBlobReference("myblob");

// Create or overwrite the "myblob" blob with contents from a local file.
using (var fileStream = System.IO.File.OpenRead(@"path\myfile"))
{
    blockBlob.UploadFromStream(fileStream);
}

this code is working fine, but what I need is uploading of file with FileUpload control in ASP.Net. I need to alter the path, and replace it with the path of the file that is on FileUpload. But what I found out in my research is that the FileUpload will not return the full path of the file. Is there anyway that the uploading of file is done by the FileUpload? I don't know how to upload it with FileUpload. Can anyone help me?

Upvotes: 2

Views: 5379

Answers (3)

Dennis Burton
Dennis Burton

Reputation: 3332

There are a couple missing details in your solution. Content Type comes to mind. There is a solid solution on this question

Upvotes: 0

Dustine Tolete
Dustine Tolete

Reputation: 471

Solved my Problem:

I replaced the code

using (var fileStream = System.IO.File.OpenRead(@"path\myfile"))
{
    blockBlob.UploadFromStream(fileStream);
}

to

using (fileASP.PostedFile.InputStream)
{
blockBlob.UploadFromStream(fileASP.PostedFile.InputStream);
}

the fileASP is the ID of the FileUpload control. It now works fine.

Upvotes: 4

Jon Susiak
Jon Susiak

Reputation: 4978

Try this:

if (FileUpload1.HasFile)
{
    blockBlob.Properties.ContentType = FileUpload1.PostedFile.ContentType;
    blockBlob.UploadFromStream(FileUpload1.FileContent);
}

Upvotes: 0

Related Questions