由于某种原因,Blob不是Blob

时间:2019-07-23 13:48:09

标签: javascript angular typescript

我分配了一个Blob对象,然后尝试在FileReader中使用该对象,但是它失败并出现“ not a blob”错误。

特别是强制转换为Blob无法正常工作。

在此处创建Blob(第一步),并在第二步中使用

stepOneHtml2canvas() {
  return new Promise<Blob>((resolve, reject) => {
    const data = document.getElementById('body');
    const options = {};
    let myBlob: Blob;
    html2canvas(data, options).then(canvas => {
      const imgWidth = 208;
      const paUghgeHeight = 295;
      const imgHeight = (canvas.height * imgWidth) / canvas.width;
      const heightLeft = imgHeight;
      const contentDataUrl = canvas.toDataURL('image/png');
      const pdf = new jspdf('p', 'mm', 'a4');
      const position = 0;
      pdf.addImage(contentDataUrl, 'PNG', 0, position, imgWidth, imgHeight);
      myBlob = new Blob([pdf.output('pdfobjectnewwindow')], {type: 'application/pdf'});
      this.blob = myBlob;
    });
    resolve(myBlob);
    reject('step one failed');
  });
}

stepTwoBuildEntity() {
  return new Promise<ICompiledOffer>((resolve, reject) => {
    const reader = new FileReader();
    let data;
    let entity: ICompiledOffer;
    //const myBlob: Blob = new Blob([this.blob], { type: 'application/pdf' });
    //console.log('++++ BLOB TYPE OF :', typeof(myBlob));
    reader.readAsDataURL(new Blob([this.blob], { type: 'application/pdf' }));
    console.log('++++ DATA1 : ', data);
    const myOffer = this.offer;
    let myEntity; // = this.entity;
    reader.onloadend = function () {
      data = reader.result;
      console.log('++++ DATA2 : ', data);
      if (data) {
        entity = {
          ...new CompiledOffer(),
          offer: myOffer,
          finishedOfferContentType: 'application/pdf',
          finishedOffer: data
        };
        myEntity = entity;
        console.log('++++ ENTITY CREATED : ', myEntity);
        resolve(entity);
      } else {
        reject('Failed to create Entity Data');
      }
    };
    this.entity = myEntity;
  });
}

我希望Blob在FileReader函数中呈现。 我收到此错误:ERROR Error: Uncaught (in promise): TypeError: Failed to execute 'readAsDataURL' on 'FileReader': parameter 1 is not of type 'Blob'. TypeError: Failed to execute 'readAsDataURL' on 'FileReader': parameter 1 is not of type 'Blob'.

0 个答案:

没有答案