Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 向TypeScript中的XMLHTTPRequest对象添加上载进度处理程序_Javascript_Typescript_Xmlhttprequest - Fatal编程技术网

Javascript 向TypeScript中的XMLHTTPRequest对象添加上载进度处理程序

Javascript 向TypeScript中的XMLHTTPRequest对象添加上载进度处理程序,javascript,typescript,xmlhttprequest,Javascript,Typescript,Xmlhttprequest,我正在写一个函数来上传文件。下面是示例代码: function UploadFile(caller: Event, form: HTMLFormElement) { // Prevent the default action caller.preventDefault(); // Create and open the request let ajaxRequest = new XMLHttpRequest(); ajaxRequest.open(form.method, form.actio

我正在写一个函数来上传文件。下面是示例代码:

function UploadFile(caller: Event, form: HTMLFormElement) {
// Prevent the default action
caller.preventDefault();

// Create and open the request
let ajaxRequest = new XMLHttpRequest();
ajaxRequest.open(form.method, form.action);
ajaxRequest.upload.onprogress.call(UpdateProgress);

if (ajaxRequest.readyState == 4) {
    form.reset();
    document.getElementById('UploadedFilePath').innerText = ajaxRequest.responseText;
    }

let fData = new FormData(form);
}

function UpdateProgress(req: XMLHttpRequest, ev: ProgressEvent) {
if (ev.lengthComputable) {
    let max = ev.total;
    let current = ev.loaded;

    let Percentage: number = Math.round((current * 100) / max);
    document.getElementById('UploadProgress').innerText = Percentage.toString();
    }
}

应用程序在“ajaxRequest.upload.onprogress.call(UpdateProgress);”时失败。我想我犯了一些错误。因此,有人可以指出如何正确地将处理程序分配给进度事件。

调用不是这样工作的,它需要
(this,…args)
,因此您可以使用
(UploadProgress().bind(UploadProgress))
调用UploadProgress,将其更改为
ajaxRequest.upload.onprogress=(函数(事件){UpdateProgress(null,event);}.bind(ajaxRequest);
尝试了这个“ajaxRequest.upload.onprogress=function(event){UpdateProgress(null,event);}.bind(ajaxRequest);”,但它不起作用。您删除了它使
bind
失败的两个括号,因为它在函数声明上不起作用(函数(事件){UpdateProgress(null,event);}).bind(ajaxRequest);“发生了什么错误?