Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #35259

Re: Array of Files to array of data url

Subject Re: Array of Files to array of data url
Newsgroups comp.lang.javascript
References <8dGdnZA1WKvpTbXGnZ2dnUU7-e3NnZ2d@westnet.com.au> <d6b53130-1df1-d8fc-0ac6-3949f37bfe54@PointedEars.de>
From Andrew Poulos <ap_prog@hotmail.com>
Date 2018-06-21 15:33 +1000
Message-ID <N4OdnS1AXoc3pbbGnZ2dnUU7-bfNnZ2d@westnet.com.au> (permalink)

Show all headers | View raw


On 21/06/2018 5:47 AM, Thomas 'PointedEars' Lahn wrote:
> Andrew Poulos wrote:
>> var newarr = files.map(function(el){
>>     if (el === null) return null;
>>
>>     var reader = new FileReader();
>>
>>     reader.addEventListener("load", function () {
>                 ^^^^^^^^^^^^^
>>       return reader.result;
>>     }, false);
>>
>>     reader.readAsDataURL(el);
>> });
>>
>> but the array only ever gets populated with null. How can this be done?
> 
> It is the (unfortunately common) mistake of expecting something to work
> synchronously when it *clearly* does not.  With minimum changes, untested:

When things fail (and I miss the forest for the trees) I run out of 
options :-(

>    var newarr = []:
>    var loaded = 0;
> 
>    function allLoaded ()
>    {
>      … newarr …
>    }
> 
>    files
>      .forEach(function (el, i, a) {
>        /* consider: if (!el) */
>        if (el === null) return;
> 
>        var reader = new FileReader();
> 
>        reader.addEventListener("load", function () {
>          newarr[i] = reader.result;
>          ++loaded;
>          if (loaded === a.length) allLoaded();
>        }, false);
> 
>        reader.readAsDataURL(el);
>      });
> 
> If you can live with the fact that the indexes do not match, you should
> filter the array so that the callback is not called for null elements in
> the first place and call Array.prototype.push() instead.
> 
> If you want to process the results in any case (whether successful or not),
> you have to handle the “error” event and increase in its listener the
> “loaded” counter as well (you should rename it then).

Thanks, it's similar to what glathoud suggested.

Andrew Poulos

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Array of Files to array of data url Andrew Poulos <ap_prog@hotmail.com> - 2018-06-19 19:44 +1000
  Re: Array of Files to array of data url Jake Jarvis <pig_in_shoes@yahoo.com> - 2018-06-19 12:28 +0200
  Re: Array of Files to array of data url Jon Ribbens <jon+usenet@unequivocal.eu> - 2018-06-19 14:47 +0000
  Re: Array of Files to array of data url Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2018-06-20 21:47 +0200
    Re: Array of Files to array of data url Andrew Poulos <ap_prog@hotmail.com> - 2018-06-21 15:33 +1000
  Re: Array of Files to array of data url "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2018-06-21 15:16 -0700

csiph-web