无法在“ URL”上执行“ createObjectURL”-Angular:

时间:2019-10-31 22:24:20

标签: angular typescript google-chrome

Chrome版本:版本78.0.3904.70(正式版本)(64位)

所以我知道URL.createObject URL从Chrome 71开始就已被弃用,但只是现在才遇到错误。

下面是我使用URL.createObject的预加载媒体功能的片段

// Loads a file from the server into cache
private loadFile(media: File) {
  console.log('Entered MediaModel loadFIle');

    return new Promise((resolve, reject) => {
        var error: boolean = false;

        var request = new XMLHttpRequest();
        request.open('GET', media.url);
        request.responseType = 'blob';

        // When the request loads, check whether it was successful
        request.onload = () => {
            if (request.status === 200) {
                resolve(request.response);
            } else {
              // If it fails, reject the promise with a error message
              reject(Error('Media didn\'t load successfully; error code:' + request.statusText));
            }
        };


        // If an error occurs
        request.onerror = () => {
            reject(Error('There was a network error.'));
        };


        request.onreadystatechange = () => {
            if (request.readyState === 4) {

                media.blob = URL.createObjectURL(request.response);
                media.sanitizedBlob = this.sanitizer.bypassSecurityTrustUrl(URL.createObjectURL(request.response));

                console.log(media);
                console.log(media.blob);
                console.log(media.sanitizedBlob);
            }
        };


        // Every tick of the progress loader
        request.onprogress = data => {
            media.loadProgress = parseInt((data.loaded / data.total * 100).toFixed(0));
        }

        // Send the request
        request.send();
    });
}

Screenshot of console.logs

我以为我可以将其全部包装在try catch块中,但随后我认为媒体不会预加载。

我读过的一些帖子/论坛建议使用MediaStream,但是我不清楚要替换或修改的内容不仅可以消除错误,而且可以使功能保持相对相同。

任何帮助将不胜感激,如果您需要更多信息,请告诉我。在此先感谢:)

0 个答案:

没有答案