如何在angularjs中将图像从base64string转换为byte[]数组

ryevplcw  于 2021-09-29  发布在  Java
关注(0)|答案(2)|浏览(384)

我正在尝试将图像转换为字节数组。下面是我返回该图像的base64string的代码:

$scope.imageAdded = function (files) {
        angular.forEach(files,
            function (flowFile, i) {
                var fileReader = new FileReader();
                fileReader.onload = function (event) {
                    var uri = event.target.result;
                    $scope.imageStrings[i] = uri;
                };
                fileReader.readAsDataURL(flowFile.file);
                event.preventDefault(); //prevent file from uploading
            });
    }

任何形式的帮助都将不胜感激

eqoofvh9

eqoofvh91#

试试这个

$scope.base64toByteArray = function(b64Data, contentType = '', sliceSize = 512) => {
        const byteCharacters = atob(b64Data);
        const byteArrays = [];

        for (let offset = 0; offset < byteCharacters.length; offset += sliceSize) {
          const slice = byteCharacters.slice(offset, offset + sliceSize);

          const byteNumbers = new Array(slice.length);
          for (let i = 0; i < slice.length; i++) {
            byteNumbers[i] = slice.charCodeAt(i);
          }

          const byteArray = new Uint8Array(byteNumbers);
          byteArrays.push(byteArray);
        } 
        return byteArrays;
      }
6ss1mwsb

6ss1mwsb2#

$scope.imageAdded = async function imageAdded (flowFiles) {
    const arrayBuffer = await flowFiles[0].file.arrayBuffer()
    const uint8array = new Uint8Array(arrayBuffer)
}

相关问题