danialfarid / ng-file-upload

Lightweight Angular directive to upload files with optional FileAPI shim for cross browser support
MIT License
7.87k stars 1.6k forks source link

Invalid Checksum during #2139

Open giper45 opened 3 years ago

giper45 commented 3 years ago

Hi, I am using ng-file library to support upload of files. I have an issue relating to binary files that is described at the following issue: https://github.com/giper45/DockerSecurityPlayground/issues/51

In order to troubleshoot, I try to log md5sum of a binary file. When I upload a binary file, I perform the following operation:

  <div class="modal-body" id="modal-body">
    <div class="btn btn-default" ngf-select="upload($files)" ngf-multiple="true">Add File</div>
    Drop File:
    <div ngf-drop ngf-select ng-model="files" class="drop-box" 
                                              ngf-drag-over-class="'dragover'" ngf-multiple="true" ngf-include-dir="true" ngf-allow-dir="true"
                                                                                                                          >Drag and Drop file</div>
    <div ngf-no-file-drop>File Drag/Drop is not supported for this browser</div>
    <!-- Files: -->
    <!-- <ul> -->
    <!--   <li ng-repeat="f in files" style="font:smaller">{{f.name}} {{f.$error}} {{f.$errorParam}}</li> -->
    <!-- </ul> -->
    Upload Log:
    <pre>{{log}}</pre>

Controller ->

      $scope.upload = function (files) {        
        if (files && files.length) {
          for (var i = 0; i < files.length; i++) {
            var file = files[i];

            _manageUpload(file);
... 

function _manageUpload(file) {
          _getFileContent(f, function(err, data) {
   ....
}
function _getFileContent(f, cb) {
        var reader = new FileReader();
        // reader.readAsText(f, "UTF-8");
        reader.readAsBinaryString(f);
        reader.onload = function (evt) {
          var md5Hash = CryptoJS.MD5(evt.target.result);
          console.log(md5Hash.toString())

          cb(null, evt.target.result);
        }
        reader.onerror = function (evt) {
          cb(new Error("error reading file"));
        }
      }

}
 ... 

}

The following code outputs md5 of a file: image

Anyway, this md5 differs from the original md5sum: image Wrong file is properly saved on server-side (it contains the same md5 of acquired file).
Do you have any suggestion to solve this? Thanks in advance, GP