Skip to content

Commit

Permalink
fix(frontend): bytes decode when load file finished (#338)
Browse files Browse the repository at this point in the history
# Problem

When use bytes to take the data, if I didn't click on the input, it will
not decode the base64 string.

# Fix
I added a Base64 encode step once the file upload is complete.
Additionally, when the input field is manually focused and modified, the
decode process will be triggered again.
  • Loading branch information
GoldenSheep402 authored Nov 28, 2024
1 parent b0c061d commit 56053cd
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions internal/resources/webform/webform.js
Original file line number Diff line number Diff line change
Expand Up @@ -1472,16 +1472,28 @@ window.initGRPCForm = function(services, svcDescs, mtdDescs, invokeURI, metadata
box.append(lbl);
container.append(box);

var input = new Input(parent, [], value);
fileInput.on('change', function() {
var reader = new FileReader();
reader.addEventListener("load", function () {
inp.text(btoa(this.result));
inp.focus();
var base64Str = '';
if (typeof this.result == 'string') {
base64Str = btoa(this.result)
} else if (this.result instanceof ArrayBuffer) {
var bytes = new Uint8Array(this.result);
var len = bytes.byteLength;
var binary = '';
for (var i = 0; i < len; i++) {
binary += String.fromCharCode( bytes[ i ] );
}
base64Str = btoa(binary);
}
inp.text(base64Str);
input.setValue(base64Str);
}, false);
reader.readAsBinaryString(fileInput[0].files[0]);
})
});

var input = new Input(parent, [], value);
inp.focus(function() {
var inp = this;
setValidation(inp, function() {
Expand Down

0 comments on commit 56053cd

Please sign in to comment.