admin管理员组文章数量:1379654
I'm trying to send a file in chunks to be saved in a database and I'm using FileReader.readAsArrayBuffer() to trigger the onload event. My issue es into play once I'm in the onload event, the scope of 'this' only contains the FileReader properties and nothing from my class. Even variables defined just before the onload event are not accessible. I was thinking I could try to pass the value of 'this' through as a parameter so I could access my functions, service and variables but I haven't had any success thus far. Has anyone tried anything similar or know if maybe I've reached some sort of limitation?
Thank you for any help
Here's my ponent class
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);
}
});
}
}
}
my ponent template
<input type="file" [(value)]="fileData" [(files)]="files" id="fileToUpload" />
<input type="button" (click)="uploadFile()" value="Upload File" />
I'm trying to send a file in chunks to be saved in a database and I'm using FileReader.readAsArrayBuffer() to trigger the onload event. My issue es into play once I'm in the onload event, the scope of 'this' only contains the FileReader properties and nothing from my class. Even variables defined just before the onload event are not accessible. I was thinking I could try to pass the value of 'this' through as a parameter so I could access my functions, service and variables but I haven't had any success thus far. Has anyone tried anything similar or know if maybe I've reached some sort of limitation?
Thank you for any help
Here's my ponent class
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);
}
});
}
}
}
my ponent template
<input type="file" [(value)]="fileData" [(files)]="files" id="fileToUpload" />
<input type="button" (click)="uploadFile()" value="Upload File" />
Share
Improve this question
asked May 9, 2016 at 19:44
user2592413user2592413
651 silver badge8 bronze badges
1 Answer
Reset to default 9Use () =>
instead of function()
to retain the scope of this
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);
}
});
}
}
See also https://developer.mozilla/de/docs/Web/JavaScript/Reference/Functions/Arrow_functions
本文标签: javascriptFileReader onload not within controller scope Angular 2Stack Overflow
版权声明:本文标题:javascript - FileReader onload not within controller scope Angular 2 - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744409256a2604867.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论