In Angular, I want to send a file using Base64 String and decode it on Asp.net Core

Viewed 20

In angular, I am making a form like this,

form = this.fb.group({
product_Id: [0]
product_Name: [''],
product_Image: ['']
})

Now I want to pass the product image in base64 string. So I have converted it using below,

fileSelected(files: FileList)
{
let file = <File>files[0]

let reader = new FileReader()
reader.readAsDataURL(file)
reader.onloadend = () => {
this.form.get('product_Image').setValue(reader.result)
}
}

On asp.net core side, I have implemented below logic,

var path = Directory.GetCurrentDirectory() + "wwwroot\\Product\\test.jpg";

await File.WriteAllBytesAsync(path, Convert.FromBase64String(vm.Product_Image));

Now the asp.net core is giving me error: "The given format is not a valid base 64 string"

PS: I have also reader.readAsBinaryString(file) but the error is the same.

The Error: The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters.

I don't know what wrong?

0 Answers
Related