简体   繁体   English

如何在forEach循环中使用JavaScript FileReader?

[英]How to use JavaScript FileReader in a forEach loop?

 let base64Data: string;
 let attachment: Attachment;  
 let blob: Blob;
 docList.forEach(([pdfDoc, title]) => {
            blob = pdfDoc.output('blob'); 
            var reader = new FileReader();
            reader.readAsDataURL(blob);
            reader.onloadend = function() {
              base64data = reader.result;
              attachment = new Attachment();
              attachment.setFilename(title);
              attachment.setContent(base64data);
              attachment.setType('application/pdf');
              attachments.push(attachment);
            }
         });

pdfDoc is a jsPDF ; pdfDocjsPDF ; Attachment is my own class with fields as indicated. 附件是我自己的课程,带有指示的字段。

If I run the above code in debug mode and add breakpoints, the attachments array is populated as I expected. 如果我在调试模式下运行上述代码并添加断点,则按我的预期填充附件数组。 Otherwise the array ends up blank. 否则,数组将最终为空白。 I know there is an issue with synchronizing the looping and the the FileReader . 我知道同步循环和FileReader存在问题 I found the following answer 我找到以下答案

Looping through files for FileReader, output always contains last value from loop 遍历FileReader的文件,输出始终包含循环中的最后一个值

but I'm not sure how to apply it to my case. 但我不确定如何将其应用于我的案子。 Any suggestions? 有什么建议么? Thanks in advance. 提前致谢。

I reckon the main problem is, that you kill your array attachment with each loop on the one hand and on the other hand you simply missed the plural s. 我认为主要的问题是,您一方面杀死每个循环的数组attachment ,另一方面却错过了复数s。

 let base64Data: string;
 let attachment: Attachment;  <<== plural s is missing
 let blob: Blob;

 docList.forEach(([pdfDoc, title]) => {
        blob = pdfDoc.output('blob'); 
        var reader = new FileReader();
        reader.readAsDataURL(blob);
        reader.onloadend = function() {
          base64data = reader.result;
          attachment = new Attachment(); <<== kills the array and overwrites it
          attachment.setFilename(title);
          attachment.setContent(base64data);
          attachment.setType('application/pdf');
          attachments.push(attachment); <<== never writes the value anywhere
        }
     });

So try it this way: 因此,请尝试这种方式:

 let attachments: Attachment; // with plural s

 docList.forEach(([pdfDoc, title]) => {
        const blob = pdfDoc.output('blob'); 
        const reader = new FileReader();
        reader.readAsDataURL(blob);
        reader.onloadend = function() {
          const base64data = reader.result;
          const attachment = new Attachment(); // without plural s
          attachment.setFilename(title);
          attachment.setContent(base64data);
          attachment.setType('application/pdf');
          attachments.push(attachment); // writes to the intended array

          // how to know when the last item was added?
          if(attachments.length === docList.length) {
              showListContent();
          }
        }
     });

     function showListContent() {
         console.log(attachments);
     }

Avoid variables with unnecessarily broad scopes as often as you can. 尽可能避免使用不必要的范围广泛的变量。 function scoped variables should always be your first choice, if applicable. 如果适用,函数范围变量应始终是您的首选。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM