-3

How to convert a Blob into a byte array? I was trying to upload a video and call to API in byte array format

After doing the below code, we getting the video in "blob:http://localhost:8080/2d118b17-34b3-4e19-8044-72e9a0c5eaff"" formate. I want to convert the blob url into byte array.

Can anyone please help me to convert bloburl into byte array?

My HTML:

<video ng-if="item.documentType == 'video'" controls  width="100" height="145">
     <source data-ng-src="{{item.data}}">
     Your browser does not support the video tag.
</video>

My JS:

if(type == 'video'){            
    var item = {
        name:file.files[0].name,
        documentType:type,
        src: file.files[0],
        data: URL.createObjectURL(file.files[0]),
        caseFileId: $scope.mediaItem.caseId
    }

    $scope.mediaItem.listDocumentDto.push(item);            
}
georgeawg
  • 48,608
  • 13
  • 72
  • 95
Ranjan Singh
  • 123
  • 2
  • 9

1 Answers1

0

You need to use FileReader to get the contents of the file into a ArrayBuffer which you can then use a TypedArray of your choice.

var fr = new FileReader();
fr.onloadend = function(){
  //fr.result will be an ArrayBuffer so create an Int8Array view
  var data = new Int8Array(fr.result);
  //use data as you wish.
};
fr.readFileAsArrayBuffer(file.files[0]);
Patrick Evans
  • 41,991
  • 6
  • 74
  • 87