0

I'm having troubles uploading doc and docx files, because I can't get the right type of file when converting it to base64. This is my input:

  <input
    accept=".pdf,.txt,.doc,.docx"
    type="file"
    (change)="onNativeInputFileSelect($event)"
    #inputFile
    hidden
  />

In my method I do this:

  onNativeInputFileSelect(event) {
    if (event.target.value) {
      const file: File = event.target.files[0];
      this.changeFile(file).then((base64: string): any => {
        this.attachment.content= base64;
        this.uploadFile(this.attachment);
      });
    }
  }

then in "uploadFile" I send the file to server as base 64 string.
The changeFile methods is this:

  changeFile(file) {
    return new Promise((resolve, reject) => {
      const reader = new FileReader();
      reader.readAsDataURL(file);
      reader.onload = () => resolve(reader.result);
      reader.onerror = (error) => reject(error);
    });
  }

This works fine for pdf or txt files, but when I upload .doc or .docx files I get this base64 string:

data:application/octet-stream;base64,0M8R4KGxGuEAA[...]

while for pdf I correctly have:

data:application/pdf;base64,JVBERi0xLjQKJeLjz9MKMS

This means that when I try to download the file I don't get the correct type, because I have application/octet-stream, and so I can't put the right extension to the file.
I've tried by passing to the accept property of input component this:

application/msword,application/pdf,text/plain,application/vnd.openxmlformats-officedocument.wordprocessingml.document

But nothing changed and the Window's file chooser suggested me only .pdf and .txt files

EDIT

This is a working stackblitz

Usr
  • 2,628
  • 10
  • 51
  • 91
  • This is working fine on my mac. "CONTENUTO data:application/vnd.openxmlformats-officedocument.wordprocessingml.document;base64,UEsDBB...." – Filipe Mendes Nov 21 '20 at 19:48

1 Answers1

0

I get "data:application/vnd.openxmlformats-officedocument.wordprocessingml.document;base64,UEsDBB[...]" for docx and "data:application/msword;base64,0M8R4KGxGuv[...]" for doc file.