0

I write a web app that on one screen the uses can upload document and on a second screen he can view this document.
The document is saved well in a binary array.
And now I have to convert it back into the document. I'm using stackoverflow.com/questions/35038884/download-file-from-bytes-in-javascript to solve it,

But i'm get this error -

DOMException: Failed to execute 'atob' on 'Window': The string to be decoded is not correctly encoded.

I'm think that is because the string is not base 64.

What can i do to solve it?
This is the code that i use to convert the file to byte[]

I try to convert the - $fileContent to file again.

 pniotApp.directive('onReadFile', function ($parse) {
    return {
        restrict: 'A',
        scope: false,
        link:function (scope, element, attrs) {
            var fn = $parse(attrs.onReadFile);

            element.on('change',function (onChangeEvent) {

                var files = (onChangeEvent.srcElement || onChangeEvent.target).files;

                for (j = 0; j < files.length; j++) {
                    readerEngine(files[j]);
                }


                function readerEngine(file_) {

                    var reader = new FileReader();
                    reader.onload = function (onLoadEvent) {
                        var buffer = onLoadEvent.target.result;
                        var uint8 = new Uint8Array(buffer);
                        var result = [];
                        for (var i = 0; i < uint8.length; i++) {
                            result.push(uint8[i]);
                        }
                        scope.$apply(function () {
                            fn(scope,{
                                $fileContent:result,
                                $fileName: file_.name,
                                $fileSize:file_.size
                            });
                        });
                    };

                    reader.readAsArrayBuffer(file_);
                }
            });
        }
    };
});
Liam
  • 27,717
  • 28
  • 128
  • 190
Tzof
  • 193
  • 12

0 Answers0