0

I'm using the following javascript solution to get the number of pages of a file :

const reader = new FileReader()
reader.readAsBinaryString(file)
reader.onloadend = function () {
 const count = reader.result.match(/\/Type[\s]*\/Page[^s]/g).length
 console.log('Number of Pages:', count)
}

The number of pages is correct in the console but I don't know how to extract that number from the scope of the reader so I can use it elsewhere.

Michal Levý
  • 33,064
  • 4
  • 68
  • 86
Raikyn
  • 133
  • 3
  • 12
  • `reader.onloadend` is likely asynchronous. So you will need to follow an async style to access the variable. – evolutionxbox Aug 04 '21 at 15:08
  • the suggested similar question is a bit confusing for me, I understand that I have to found a way to wait for the result but I don't understand how to do it – Raikyn Aug 04 '21 at 15:19
  • Take a look at the accepted answer. Look at step 2 specifically. – evolutionxbox Aug 04 '21 at 15:21
  • Honestly this is beyond my capabilities for now, I kind of understand the solution they wrote but I failed to see how to use it in my case. – Raikyn Aug 04 '21 at 15:32
  • `request.onload` in the answer is almost identical to `reader.onloadend`? – evolutionxbox Aug 04 '21 at 15:35
  • Sorry I don't understand, is it in the accepted answer? When you say step 2, are you referring to the callback solution? – Raikyn Aug 04 '21 at 15:53
  • Yes that is the solution I was referring to. Since `onloadend` is a callback, it's a solution that seems to fit the best. – evolutionxbox Aug 04 '21 at 15:54
  • So should i do something like ```reader.onloadend = function callback () { ... return count }``` ? – Raikyn Aug 04 '21 at 16:01
  • Take a look at https://stackoverflow.com/questions/54951205/how-to-implement-a-callback-in-javascript-onload-event even though it talks about `window.onload` the approach is similar to what you need. – evolutionxbox Aug 04 '21 at 16:04
  • I've tried to write the code but couldn't make it work, I thought I understand how it works but nop – Raikyn Aug 05 '21 at 09:03

0 Answers0