Reputation: 25
So my problem is that I need to read a file from a webform published in azure, the user select the file using asp.net FileUpload, but I can't read it without saving, I need to save in order to read it. How to do that using azure? Is there another way to read the file without saving it?
Thanks!
Upvotes: 0
Views: 143
Reputation: 131712
You don't need to save an uploaded files in order to read it. The raw contents are available as a byte stream through the FileContents property. The PostedFile property offers more information like the file length and content type. You can read the contents through its InputStream property.
SaveAs simply reads the byte stream and writes the bytes to a file.
If the file is small, you can access its bytes directly through the FileBytes property. Otherwise you can read bytes from the stream and copy them to another stream.
You can create a CloudBlockBlob with data coming from a stream with UploadFromStream:
var connectionString=CloudConfigurationManager.GetSetting("StorageConnectionString");
var acct = CloudStorageAccount.Parse(connectionString);
var client = acct.CreateCloudBlobClient();
var blob = container.GetBlockBlobReference(fileName);
blob.UploadFromStream(FileUpload1.FileContents);
In general, file and blob operations always offer a way to use a stream. It would be impossible to work with large files otherwise
Upvotes: 1