Javascript FileReader onload不在控制器作用域2内

Javascript FileReader onload不在控制器作用域2内,javascript,typescript,angular,Javascript,Typescript,Angular,我试图将文件分块发送到数据库中,并使用FileReader.readAsArrayBuffer()触发onload事件。一旦我在onload事件中,“this”的作用域只包含FileReader属性,而我的类中没有任何内容。甚至在onload事件之前定义的变量也不可访问。我想我可以尝试将“this”的值作为参数传递,这样我就可以访问我的函数、服务和变量,但到目前为止我还没有成功。有没有人尝试过类似的方法,或者知道我是否已经达到某种程度的限制 谢谢你的帮助 这是我的组件类 export class

我试图将文件分块发送到数据库中,并使用FileReader.readAsArrayBuffer()触发onload事件。一旦我在onload事件中,“this”的作用域只包含FileReader属性,而我的类中没有任何内容。甚至在onload事件之前定义的变量也不可访问。我想我可以尝试将“this”的值作为参数传递,这样我就可以访问我的函数、服务和变量,但到目前为止我还没有成功。有没有人尝试过类似的方法,或者知道我是否已经达到某种程度的限制

谢谢你的帮助

这是我的组件类

export class UploadComponent {
title = 'Upload File';
fileData = null;
files = null;
fs;
@Input()
showUpload: boolean;

constructor(fileService: FileService) {
    this.fs = fileService;
}

uploadFile() {
    let temp = document.getElementById("fileToUpload");
    let fr = new FileReader();
    let file = temp.files[0];
    let fb = fr.readAsArrayBuffer(file);

    fr.onload = function(data) {
        this.fs.beginFileUpload(file.name, function(data) {
            let chunkSize = 200000;
            let startIndex = 0;
            let stopIndex = chunkSize;
            let file = data.target.result;
            let fileChunk = null;

            while (startIndex < file.length) {
                if (stopIndex < length) {
                    fileChunk = file.subArray(startIndex, stopIndex);
                    startIndex = stopIndex;
                    stopIndex += chunkSize;
                }
                else {
                    fileChunk = file.subArray(startIndex);
                    startIndex = stopIndex;
                }
                //fs.uploadChunk(fileChunk);
            }
        });

    }

}
导出类上传组件{
标题='上传文件';
fileData=null;
files=null;
财政司司长;
@输入()
showUpload:布尔;
构造函数(文件服务:文件服务){
this.fs=fileService;
}
上传文件(){
让temp=document.getElementById(“fileToUpload”);
设fr=newfilereader();
let file=temp.files[0];
设fb=fr.readAsArrayBuffer(文件);
fr.onload=函数(数据){
this.fs.beginfeupload(file.name,函数(数据){
让chunkSize=200000;
设startIndex=0;
设stopIndex=chunkSize;
让file=data.target.result;
让fileChunk=null;
while(startIndex
}

我的组件模板

<input type="file" [(value)]="fileData" [(files)]="files" id="fileToUpload" />
<input type="button" (click)="uploadFile()" value="Upload File" />

使用
()=>
而不是
函数()
来保留

uploadFile() {
    let temp = document.getElementById("fileToUpload");
    let fr = new FileReader();
    let file = temp.files[0];
    let fb = fr.readAsArrayBuffer(file);

    fr.onload = (data) => {
        this.fs.beginFileUpload(file.name, (data) => {
            let chunkSize = 200000;
            let startIndex = 0;
            let stopIndex = chunkSize;
            let file = data.target.result;
            let fileChunk = null;

            while (startIndex < file.length) {
                if (stopIndex < length) {
                    fileChunk = file.subArray(startIndex, stopIndex);
                    startIndex = stopIndex;
                    stopIndex += chunkSize;
                }
                else {
                    fileChunk = file.subArray(startIndex);
                    startIndex = stopIndex;
                }
                //fs.uploadChunk(fileChunk);
            }
        });

    }
}
uploadFile(){
让temp=document.getElementById(“fileToUpload”);
设fr=newfilereader();
let file=temp.files[0];
设fb=fr.readAsArrayBuffer(文件);
fr.onload=(数据)=>{
this.fs.beginfeupload(file.name,(data)=>{
让chunkSize=200000;
设startIndex=0;
设stopIndex=chunkSize;
让file=data.target.result;
让fileChunk=null;
while(startIndex

另请参见成功的

,非常感谢!这样一个简单的解决方案。谢谢你的链接,我还有更多的学习要做。时间一到,我就把它标记为已回答哈哈