I have an upload section in my JavaScript program. I utilize JS FileReader to obtain a binary string of the uploaded document before sending it to my C# WebApi for storage on the server.
JavaScript Code
let myFile = ev.target.files[0];
if(myFile.size > 0){
let reader = new FileReader();
var fileByteArray = [];
reader.readAsArrayBuffer(myFile);
reader.onloadend = (e) => {
var buffer = <ArrayBuffer>reader.result;
var uintArray = new Uint8Array(buffer);
var binaryString = String.fromCharCode.apply(null, uintArray);
let resourceModel = new Model({
contentType: myFile.type,
fileName: myFile.name,
fileContent: binaryString
});
}
}
C# Code:
if (!String.IsNullOrEmpty(model.fileContent))
{
byte[] bytes = Encoding.UTF8.GetBytes(model.FileContent);
File.WriteAllBytes(RESOURCES_SAVE_PATH, bytes);
}
Although no errors occur during execution, attempting to open the file results in unrecognized content. Any suggestions on how to fix this issue?