vb.net - Displaying progress while uploading data -
i using following code located here upload files
public function uploadfile(byval ofile fileinfo) boolean dim ftprequest ftpwebrequest dim ftpresponse ftpwebresponse try ftprequest = ctype(ftpwebrequest.create(ftpsite + currentdirectory + ofile.name), _ ftpwebrequest) ftprequest.method = webrequestmethods.ftp.uploadfile ftprequest.proxy = nothing ftprequest.usebinary = true ftprequest.credentials = new networkcredential(username, password) ftprequest.keepalive = keepalive ftprequest.enablessl = usessl if usessl servicepointmanager.servercertificatevalidationcallback = _ new remotecertificatevalidationcallback(addressof validateservercertificate) dim filecontents(ofile.length) byte using fr filestream = ofile.openread fr.read(filecontents, 0, convert.toint32(ofile.length)) end using using writer stream = ftprequest.getrequeststream writer.write(filecontents, 0, filecontents.length) end using ftpresponse = ctype(ftprequest.getresponse, ftpwebresponse) ftpresponse.close() ftprequest = nothing return true catch ex webexception return false end try end function
i extend it, can have upload progress too. problem not know start. "logic" of displaying upload progress?
"split file" in predefined parts , upload them or what?
you need execute upload request on background thread avoid blocking ui thread. easiest way using backgroundworker class. it's designed situations this.
dim backgroundworker new system.componentmodel.backgroundworker() backgroundworker.workerreportsprogress = true backgroundworker.workersupportscancellation = true addhandler backgroundworker.dowork, addressof me.backgroundfiledownload addhandler backgroundworker.progresschanged, addressof me.progresschanged addhandler backgroundworker.runworkercompleted, addressof me.jobcompleted
the events progresschanged , runworkercompleted run on ui thread, , allow update progress bar accordingly current download's status. they'll this:
protected sub progresschanged(byval sender object, byval e progresschangedeventargs) progressbar.value = e.progresspercentage end sub
dowork called on background thread, , want call uploadfile() function you've written. ftpwebrequest, you're going want first file's size, upload blocks of data, divide you've uploaded far file's full size percentage complete. like
worker.reportprogress(math.round((_bytesuploaded / _filesize) * 100))
hope helps.
Comments
Post a Comment