Skip to content
Advertisement

How to get bytecode from selected pdf in angular

I want to extract the bytecode from an file I select (pdf) to save it in my database. But I am always getting the error that my byte is undefined. Could someone look at my code and tell me what is wrong with it?

I tried to use the FileReader but my byte returns undefined, the formdata works fine it shows on the console every information I need for the file.

EDIT:

uploadFile2(files: FileList | null): void {
        const file = files.item(0)
        const reader = new FileReader()
        reader.readAsDataURL(file)
        reader.onload = () => {
          this.documentsArray.push({documentType: this.form.controls.dokumentType.value, file: reader.result})
          console.log(this.documentsArray)
        }
    }

Hi I edited my code and now I am getting a base64 I think, but one question, it starts like this: data:application/pdf;base64,JVBERi0xLjQKJfbk/N8KM…….”

is the start with data:application/pdf correct or do I have to change something to save it in the database

Advertisement

Answer

I’d suggest you to store the file as a base64 String in your database. This would look as following. With the line number 2 you’re fetching the file from your input event.

    const reader = new FileReader();
    reader.readAsDataURL(event.target.files[0]);
    reader.onload = (event) => {
    if (reader.result) {
      //save pdf base64 into database
    }
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement