Getting upload progress using Azure Storage in Android -
i'm uploading file in android application. code pretty simple:
private boolean uploadfile(string filelocation) { try { if (textutils.isempty(filelocation)) { return false; } file fsrc = new file(filelocation); if (!fsrc.exists()) { return false; } boolean breturn = azuremanager.init(this); if (!breturn) { return false; } string blobname = fsrc.getname(); inputstream in = new bufferedinputstream(new fileinputstream(fsrc)); cloudblobcontainer container = azuremanager.getcloudblobclient().getcontainerreference(azuremanager.getcontainername()); cloudblockblob blob = container.getblockblobreference(blobname); blob.upload(in, fsrc.length()); in.close(); return true; } catch (exception e) { //handle exception } return false; }
when download azure, cloudblockblob has download listener as:
blob.setdownloadlistener(eventlistener);
but how can keep track of progress when uploading?
i finding way in java or android. but, if want make own way, without changing on server, can make similar this answer. answer in c# need find similar method java library , update accordingly.
if don't want go on answer, can refer same code here well.
using microsoft.windowsazure.storage; using microsoft.windowsazure.storage.auth; using microsoft.windowsazure.storage.blob; using system; using system.collections.generic; using system.io; using system.linq; using system.text; using system.threading; using system.threading.tasks; namespace consoleapplication1 { class program { static cloudstorageaccount storageaccount = new cloudstorageaccount(new storagecredentials("accountname", "accountkey"), true); static void main(string[] args) { cloudblobclient myblobclient = storageaccount.createcloudblobclient(); myblobclient.singleblobuploadthresholdinbytes = 1024 * 1024; cloudblobcontainer container = myblobclient.getcontainerreference("adokontajnerneki"); //container.createifnotexists(); cloudblockblob myblob = container.getblockblobreference("cfx.zip"); var blocksize = 256 * 1024; myblob.streamwritesizeinbytes = blocksize; var filename = @"d:\cfx.zip"; long bytestoupload = (new fileinfo(filename)).length; long filesize = bytestoupload; if (bytestoupload < blocksize) { cancellationtoken ca = new cancellationtoken(); var ado = myblob.uploadfromfileasync(filename, filemode.open, ca); console.writeline(ado.status); //does not ado.continuewith(t => { console.writeline("status = " + t.status); console.writeline("it over"); //this working ok }); } else { list<string> blockids = new list<string>(); int index = 1; long startposition = 0; long bytesuploaded = 0; { var bytestoread = math.min(blocksize, bytestoupload); var blobcontents = new byte[bytestoread]; using (filestream fs = new filestream(filename, filemode.open)) { fs.position = startposition; fs.read(blobcontents, 0, (int)bytestoread); } manualresetevent mre = new manualresetevent(false); var blockid = convert.tobase64string(encoding.utf8.getbytes(index.tostring("d6"))); console.writeline("now uploading block # " + index.tostring("d6")); blockids.add(blockid); var ado = myblob.putblockasync(blockid, new memorystream(blobcontents), null); ado.continuewith(t => { bytesuploaded += bytestoread; bytestoupload -= bytestoread; startposition += bytestoread; index++; double percentcomplete = (double)bytesuploaded / (double)filesize; console.writeline("percent complete = " + percentcomplete.tostring("p")); mre.set(); }); mre.waitone(); } while (bytestoupload > 0); console.writeline("now committing block list"); var pbl = myblob.putblocklistasync(blockids); pbl.continuewith(t => { console.writeline("blob uploaded completely."); }); } console.readkey(); } } }
let me know if can more in this. thanks.
Comments
Post a Comment