Reputation: 4893
I am uploading a video on my site by taking it by client using simple uploader then uploading to azure blob
in code-behind using blob.UploadByteArray()
,
I want to track upload progress
that how many bytes are uploaded out of total at that moment? Is there any API or workaround for that?
I don't want to use third party up-loader or blob pusher etc.
Upvotes: 0
Views: 3495
Reputation: 74605
I read that blog post mentioned by CorneM but I wasn't too keen on the implementation..
Instead I subclassed FileStream so that it raised events every so often that it was read from, and provided my subclassed filestream to the UploadFromStream method on the azure storage client in the SDK. Much cleaner, IMHO
public delegate void PositionChanged(long position);
public class ProgressTrackingFileStream: FileStream
{
public int AnnounceEveryBytes { get; set; }
private long _lastPosition = 0;
public event PositionChanged StreamPositionUpdated;
// implementing other methods that the storage client may call, like ReadByte or Begin/EndRead is left as an exercise for the reader
public override int Read(byte[] buffer, int offset, int count)
{
int i = base.Read(buffer, offset, count);
MaybeAnnounce();
return i;
}
private void MaybeAnnounce()
{
if (StreamPositionUpdated != null && (base.Position - _lastPosition) > AnnounceEveryBytes)
{
_lastPosition = base.Position;
StreamPositionUpdated(_lastPosition);
}
}
public ProgressTrackingFileStream(string path, FileMode fileMode) : base(path, fileMode)
{
AnnounceEveryBytes = 32768;
}
}
And then used it like this (_container is my azure storage container, file is a FileInfo for my local file):
CloudBlockBlob blockBlob = _container.GetBlockBlobReference(blobPath);
using (ProgressTrackingFileStream ptfs = new ProgressTrackingFileStream(file.FullName, FileMode.Open))
{
ptfs.StreamPositionUpdated += ptfs_StreamPositionUpdated;
blockBlob.UploadFromStream(ptfs);
}
Upvotes: 1
Reputation: 7129
Azure Storage allows you to upload blocks to a blob. Take a look at this example on MSDN blogs.
By sending blocks of data to Azure, you can meanwhile track your progress.
Upvotes: 0
Reputation: 416
I haven't found an API to track progress. One way I have implemented the progress bar is by uploading the blob as smaller chunks to azure storage. As each chunk uploads successfully, you can variations in the progress bar based on the number of chunks.
Upvotes: 0