0
function upload(file){
    var reader = new FileReader();
    reader.onload = function (evt) {
        uploadComplet(file.name, evt.target.result);
    }
    reader.readAsBinaryString(file.fileBlob);
}



    function downloadComplet(binaryString){
        var newFile = new File( [binaryString] , "newName.jpg");

        var downloadUrl = URL.createObjectURL(newFile);
        var downloadButton = document.createElement('a');
        downloadButton.setAttribute('href', downloadUrl);
        downloadButton.setAttribute('download', newFile.name);
        downloadButton.setAttribute('class', 'button');
        downloadButton.innerText = 'Download: ' + newFile.name;
        downloadButton.click();
    }

I uploaded the BinaryString of the image file and downloaded it as it was. But I could not bring in the correct image. What should I do?

  • var newFile = new File( [new Blob([binaryString], {type: 'image/jpeg'})] , "newName.jpg"); I also tried this but it failed. – fsfmals23134 Nov 07 '17 at 13:05
  • have you tried something like https://stackoverflow.com/questions/2429934/is-it-possible-to-put-binary-image-data-into-html-markup-and-then-get-the-image ? – dr_leevsey Nov 07 '17 at 13:08
  • I want to upload and download images into the img data stream. – fsfmals23134 Nov 07 '17 at 13:28

1 Answers1

0

Take this as an example:

var img = document.createElement('img');
img.src = 'data:image/jpeg;base64,' + btoa('your-binary-data');
document.body.appendChild(img);
Barr J
  • 10,636
  • 1
  • 28
  • 46
  • 1
    It can. But I do not want to have an image on the web. I want to get the correct image file when I download the image stream. Text could be obtained in the same way. But the image was not drawn correctly. – fsfmals23134 Nov 07 '17 at 13:25
  • You could use this: var blob = new Blob([myTypedArray], {type: 'application/octet-binary'}); var url = URL.createObjectURL(blob); – Barr J Nov 07 '17 at 14:02