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