我有这个函数将文件解析为字节:
该功能可以上传多个文件。
pniotApp.directive('onReadFile', function ($parse) {
return {
restrict: 'A',
scope: false,
link:function (scope, element, attrs) {
var fn = $parse(attrs.onReadFile);
element.on('change',function (onChangeEvent) {
var files = (onChangeEvent.srcElement || onChangeEvent.target).files;
for (j = 0; j < files.length; j++) {
readerEngine(files[j]);
}
function readerEngine(file_) {
var reader = new FileReader();
reader.onload = function (onLoadEvent) {
var buffer = onLoadEvent.target.result;
var uint8 = new Uint8Array(buffer);
var result = [];
for (var i = 0; i < uint8.length; i++) {
result.push(uint8[i]);
}
scope.$apply(function () {
fn(scope, {
$fileContent: result,
$fileName: file_.name
});
});
};
reader.readAsArrayBuffer(file_);
}
});
}
};
});
在我想要使用的另一个函数中:$ fileContent,$ fileName:
$scope.saveContent = function ($fileContent, $fileName) {
$scope.contentFile.push({
name: $fileName,
content: $fileContent
});
console.log($scope.contentFile);
};
但$ fileName未定义($ fileContent包含好数据)为什么?