0 votes
950 views
in Activities & Tasks by (160 points)
Hi,
I've a BPM_File_Uploader component and wanted to show a progress bar while the document is being uploaded.

How do I calculate the progress formula based on file upload? How the file is being uploaded the progress bar will be filled..

1 Answer

0 votes
by (30.6k points)

The File Uploader control (BPM document store) posts the file with an XMLHttpRequest; the toolkit exposes progress through the control's On upload progress style events on newer versions, and on older ones you can attach to the request yourself. The formula is always loaded / total:

// UI Toolkit File Uploader "on upload started" (newer toolkits expose the xhr) - or in a custom uploader coach view
xhr.upload.addEventListener("progress", function (e) {
  if (e.lengthComputable) {
    var pct = Math.round(e.loaded * 100 / e.total);        // 0..100
    ${Progress}.setData(pct);                              // Progress Bar control bound to a number 0..100
    ${Label}.setText(Math.round(e.loaded / 1024) + " KB of " + Math.round(e.total / 1024) + " KB");
  }
});
xhr.upload.addEventListener("load", function () { ${Progress}.setData(100); });

If your toolkit level does not expose the request, build a tiny custom uploader: a file input, a FormData POST to the document REST resource, and the progress listener above:

var fd = new FormData(); fd.append("file", input.files[0]);
var xhr = new XMLHttpRequest();
xhr.open("POST", "/rest/bpm/wle/v1/document?name=" + encodeURIComponent(input.files[0].name) + "&processInstanceId=" + piid, true);
xhr.setRequestHeader("BPMCSRFToken", csrfToken);     // BAW 20+ / CP4BA
xhr.upload.onprogress = function (e) { if (e.lengthComputable) ${Progress}.setData(Math.round(e.loaded * 100 / e.total)); };
xhr.onload = function () { ${DocList}.refresh(); };
xhr.send(fd);

The file size is known before the upload from input.files[0].size (bytes), which also lets you refuse files over a limit before sending them (question 2968 covers the total across uploads).

References

Related questions

0 votes
2 answers 4.6k views
0 votes
4 answers 4.5k views
0 votes
1 answer 1.0k views
0 votes
2 answers 4.0k views
0 votes
1 answer 2.3k views
0 votes
1 answer 2.6k views
0 votes
1 answer 1.4k views
+1 vote
1 answer 1.8k views
0 votes
1 answer 2.0k views

723 questions

807 answers

98 comments

4.9k users

Join BPM Community Discord Channel

Welcome to BPM Tips Q&A, Community wiki/forum where you can ask questions and receive answers from other IBM BPM experts and members of the community. Users with 2000 points will automatically be promoted to expert level.
Created by Dosvak LLC
Our Youtube Channel
...