-1

I am trying to convert blob object into base64

var out = doc.getZip().generate({
type: "blob",
mimeType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
}) //Output the document using Data-URI

In the below console. I am getting blob object.

console.log(Blob {size: 1402, type: 'application/vnd.openxmlformats- 
officedocument.wordprocessingml.document'});

I used the blob object to convert into base64

var reader = new FileReader();
reader.readAsDataURL(out); 
reader.onloadend = function() {
var base64data = reader.result;                
console.log(base64data);
return;
} 

but I am getting issue while converting into base64

Failed to execute 'readAsDataURL' on 'FileReader': parameter 1 is not of type 'Blob'.

I used filesaver.js to allow me to download the file successfully by using saveAs() method

saveAs(out,"Details.docx");
8
  • console.log(Blob {size: 1402, type: 'application/vnd.openxmlformats- officedocument.wordprocessingml.document'}); isn't valid javascript, so ... not sure how you can be getting a blob object logged to the console Commented Jul 23, 2022 at 6:25
  • when I console them out variable. I get this "Blob {size: 1402, type: 'application/vnd.openxmlformats- officedocument.wordprocessingml.document'} " Commented Jul 23, 2022 at 8:26
  • no, that code you posted is not valid ... are you showing the result of console.log rather than the actual code? Commented Jul 23, 2022 at 8:30
  • I am showing the result of the console.log value Commented Jul 23, 2022 at 8:33
  • 1
    Please show the whole script, not just separated snippets like that . The problem is probably a closure one and out isn't the same thing when you pass it to readAsDataURL than when you logged it. Commented Jul 23, 2022 at 9:57

1 Answer 1

-1

You wanna convert a docx to base64 as I understand

HTML

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<div id="upload-button" class="fpd-btn-raised fpd-secondary-bg-color fpd-secondary-text-color">
  <i class="fpd-icon-file-upload"></i><span>Select File</span>
</div>
<input type="file" id="upload-file" class="btn btn-success" style="display:none;" />
<img src="" height="200" />

JS

$('#upload-button').click(function(){
  $('#upload-file').trigger('click');                 
  return false;
});

function convert(e) {
  if(window.FileReader) {
    var file  = e.target.files[0];
    var reader = new FileReader();

    reader.readAsDataURL(file);

    reader.onloadend = function (e) {
      console.log(reader.result);
    }
  }
}

$(document).ready(function() {
  document.getElementById('upload-file').addEventListener('change', convert, false);
  
});

Pen CodePen

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.