跟踪上传进度,同时上传文件到azure blob
问题描述:
我上传一个视频在我的网站上,由客户端使用简单的上传程序,然后上传到代码隐藏使用blob.UploadByteArray(),上传到azure blob我想跟踪上传在那一刻总共上传了多少字节的进度?有没有任何API或解决方法?不要第三方上传或blob推等。在此先感谢!跟踪上传进度,同时上传文件到azure blob
答
我还没有找到API来跟踪进度。我已经实现了进度条的一种方式是将blob作为较小的块上传到Azure存储。当每个块成功上传时,您可以根据块的数量改变进度条。
答
我读到CorneM提到的博客文章,但我不是在执行太热衷..
相反,我的子类的FileStream,以便它引发的事件,每隔一段时间,它正在读取,并提供我的子类filestream添加到SDK中Azure存储客户端上的UploadFromStream方法。干净多了,恕我直言
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;
}
}
然后使用它像这样(_container是我的Azure存储容器,文件是我的本地文件一个FileInfo):
CloudBlockBlob blockBlob = _container.GetBlockBlobReference(blobPath);
using (ProgressTrackingFileStream ptfs = new ProgressTrackingFileStream(file.FullName, FileMode.Open))
{
ptfs.StreamPositionUpdated += ptfs_StreamPositionUpdated;
blockBlob.UploadFromStream(ptfs);
}
这是不准确的,你只是跟踪阅读将文件存储到内存中,而不会将其上传到Azure,这需要花费更长的时间。我正在寻找一个解决方案,我认为准确的方法应该是在块级别左右。 – Hossam 2014-01-05 14:00:36