Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(frontend): bytes decode when load file finished #338

Merged
merged 5 commits into from
Nov 28, 2024

Conversation

GoldenSheep402
Copy link
Contributor

Problem

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

  fileInput.on('change', function() {
      var reader = new FileReader();
      reader.addEventListener("load", function () {
          inp.text(btoa(this.result));
      }, false);
      reader.readAsBinaryString(fileInput[0].files[0]);
  })
  
  var input = new Input(parent, [], value);
  inp.focus(function() {
      var inp = this;
      setValidation(inp, function() {
          var str = $(inp).val();
          if (!isBase64(str)) {
              throw new Error("value for type " + fld.type + " is not a valid base64-encoded string: " + JSON.stringify(value));
          }
          input.setValue(str);
      });
  });
  return input;
image

Fix

I added a Base64 decode step once the file upload is complete. Additionally, when the input field is manually focused and modified, the decode process will be triggered again.

        fileInput.on('change', function() {
            var reader = new FileReader();
            reader.addEventListener("load", function () {
                var base64Str = btoa(this.result);
                inp.text(base64Str);

                // when file is loaded, decode the base64 string and set the value
                var str = base64Str;
                if (!isBase64(str)) {
                    throw new Error("value for type " + fld.type + " is not a valid base64-encoded string: " + JSON.stringify(value));
                }
                input.setValue(str);
            }, false);
            reader.readAsBinaryString(fileInput[0].files[0]);
        });

        var input = new Input(parent, [], value);
        inp.focus(function() {
            var inp = this;
            setValidation(inp, function() {
                var str = $(inp).val();
                if (!isBase64(str)) {
                    throw new Error("value for type " + fld.type + " is not a valid base64-encoded string: " + JSON.stringify(value));
                }
                input.setValue(str);
            });
        });
        return input;

@dragonsinth
Copy link
Member

@jhump would you mind eyeballing this? I'm not very familiar with the frontend code.

@GoldenSheep402
Copy link
Contributor Author

@dragonsinth Is there any progress?

Copy link
Member

@dragonsinth dragonsinth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@GoldenSheep402 I pushed you a commit that I think should handle the ArrayBuffer case and avoid the need to call isBase64... could you test this out and let me know if it works?

@GoldenSheep402
Copy link
Contributor Author

GoldenSheep402 commented Nov 28, 2024

image

It works!

@dragonsinth dragonsinth merged commit 56053cd into fullstorydev:master Nov 28, 2024
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants