在TypeScript中将Upload Progress处理程序添加到XMLHTTPRequest对象

时间:2018-08-11 19:32:07

标签: 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.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);”处失败。我相信我在这里犯了一些错误。因此,有人可以指出如何正确地将处理程序分配给Progress Event。

0 个答案:

没有答案