Forum Discussion
harrychen1
Jan 08, 2019Copper Contributor
PnP JS to attach files to list item from SharePoint framework issue - cannot pass file content
I try to use PnP JS to attach files to list item from SharePoint framework and have issue to pass file content. 0. I'm not using any javascript framework to simplify the demo 1. Here is the H...
harrychen1
Jan 15, 2019Copper Contributor
Issue resolved after I changed the code to process files in input file element change event. All thse code will be outside default class.
The code looks like below.
var fileInfos = [];
$(document).ready(function() {
$('#inputfile').on('change', function() {
processFile();
});
});
// Try this later https://www.javascripture.com/FileReader
function processFile() {
var input = (<HTMLInputElement>document.getElementById('inputfile'));
var fileCount = input.files.length;
console.log(fileCount);
for (var i = 0; i < fileCount; i++) {
var fileName = input.files[i].name;
console.log(fileName);
var file = input.files[i];
var reader = new FileReader();
reader.onload = (function(file) {
return function(e) {
console.log(file.name);
fileInfos.push({
"name": file.name,
//"content": reader.result
"content": e.target.result
});
console.log(fileInfos);
};
}) (file);
reader.readAsArrayBuffer(file);
}