26
Views
3
Comments
Issue reading file using  JavaScript and save it to Database
Application Type
Reactive
Service Studio Version
11.54.29 (Build 62891)

I am trying to read file using JavaScript (readAsBinaryString) and save it to database as bindary data .I am not  going to use Outsystem upload widget .

My goal is to read the file  as Binary data and save to database in small chunk/part .  (For large File) 

problem : The problem is that Binary vairable  that hold file in chunk(part) shows some bytes( like 50000 byte) on client action but that  same varible I send server action  as input it show (o bytes). So some where Binary data lost from client action to server actoin .


I read file  using  var fileInput =document.getElementById($parameters.UploadFileId).fileInput.files[0]  and use readAsBinaryString reader to read binary string  and divide in chunk (part)  then call a clientaciton $actions.SendChunkDataToServer(slice)  and send that chunk variable over client action. 

When I debug I can see in that client action  it show some binary data (50000 bytes) but When I send this input to server action it start showing (o bytes) .


I have attached detail code of JavaScript  in as attachment .Also attaching video of debugging 

JavaScript code like that

var UploadElement =document.getElementById($parameters.UploadFileId);

UploadElement.addEventListener('change', function(){

  

       var fileInput =document.getElementById($parameters.UploadFileId);

        console.log(fileInput.files[0])

        console.log('Trying to upload the video file: %O', fileInput);


        if ('files' in fileInput) {

            if (fileInput.files.length === 0) {

                alert("Select a file to upload");

            } else {

                UploadVideo(fileInput.files[0]);

            }

        } else {

            console.log('No found "files" property');

        }

    });



 var fileInput =document.getElementById($parameters.UploadFileId);

 UploadVideo(fileInput.files[0]);

function UploadVideo(file) {

    var loaded = 0;

    var chunkSize = 500000;

    var total     = file.size;

    var reader    = new FileReader();

    var slice = file.slice(0, chunkSize);

    reader.readAsBinaryString(slice);      

    // Reading a chunk to invoke the 'onload' event

     console.log('Started uploading file "' + file.name + '"');

        

    reader.onload = function (e) {

        //Just simulate API

         console.log("Slice Data")

         console.log(slice)

         $actions.SendChunkDataToServer(slice);


          loaded += chunkSize;

          var percentLoaded = Math.min((loaded / total) * 100, 100);

          console.log('Uploaded ' + Math.floor(percentLoaded) + '% of file "' + file.name + '"');

         // $('#uploadVideoProgressBar').width(percentLoaded + "%");


          //Read the next chunk and call 'onload' event again

          if (loaded <= total) {

              slice = file.slice(loaded, loaded + chunkSize);

            reader.readAsBinaryString(slice);

          } else { 

              loaded = total;

            console.log('File "' + file.name + '" uploaded successfully!');

            //$('#uploadVideoProgressBar').hide();

          }

        

    }

}




UploadFileChunk.oml
00c250e3-f7b3-4248-917f-579c54787341.mp4

Hi,

There is already a Forge component that splits a file in smaller chunks and uploads them, created by the number #1 Forge component creator and MVP Miguel Antunes.

https://www.outsystems.com/forge/component-overview/9945/chunk-it

Regards,

Daniel 

Hi,

I had this problem before.

The problem is Outsystems can't read the binary data from readAsBinaryString() function. I don't know why . My guess is Binary data and Blob data type are different.

Here is what i did to overcome.

I convert to base64 using readAsDataURL(), File data now is text, chunk it and send it to server then convert back to binary.

Here is my js for upload for your reference.


function readAndSend(file) {

    var reader = new FileReader();

    reader.addEventListener('load', function() {

        var startOfOSBinary = this.result.indexOf(";base64,") + 8;

        var lengthOSBinary = this.result.length - startOfOSBinary;

        var OSbinary = this.result.substr(startOfOSBinary, lengthOSBinary);

        $actions.TriggerFileUpload(file.name, file.size, file.type, OSbinary);

    }, false);

    reader.readAsDataURL(file);

}

var fileInput = document.getElementById($parameters.FileInputId);

var files = Array.from(fileInput.files);

if (files) {

    for (var i = 0; i < files.length; i++) {

        readAndSend(files[i]);

    }

} else {

    $actions.TriggerFileUploadError("", "", "Uploading files is disabled");

}

fileInput.value = "";


I define TriggerFileUpload action for handling the file . OSBinary is a whole file as text, so i chunk it then send to server then convert back to binary.

I used this to upload large image and video too.


Hope it help.


Cheers, Nam

Thanks  @Nam Nguyen  for your reply  Yes Outsystems does not recognizing  JavaScript  Blob data . Converting base64 is last solution I am thinking  

Base64   working my side too .Actually I don't want to convert  binary to base64  and re convert to base64 to binary sever side   because it put extra overhead  if there is large big  fine .

If there is someway we can send directly binary data to server side 



Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.