bgrins / filereader.js

A lightweight wrapper for the JavaScript FileReader interface
http://bgrins.github.com/filereader.js/
MIT License
413 stars 86 forks source link

SetSync don't work #19

Open miltonmagalhaes opened 9 years ago

miltonmagalhaes commented 9 years ago

The code run like Step 1 -> Step 3 -> Step 2

I need to know the TypeArq content after reading the file, ie synchronous manner. However, the contents of TypeArq return anything.

The Code: var TypeArq = "" alert("Step 1"); FileReaderJS.setSync(true); FileReaderJS.setupInput(document.getElementById('input'), { readAsDefault: "Text", on: { load: function(e, file) { TypeArq = e.target.result.substring(1,8); console.log("Funcionou--->" + e.target.result.substring(1,8)); alert("Step 2"); } }
}); alert("Step 3"); alert(TypeArq)

bgrins commented 9 years ago

It's synchronous once the user begins the input (not on initialization). So there is an async gap between the code that sets up the reader and the user actually selecting a file. You can read more about this FileReaderSync object on MDN.

In your case, you could move 'step 3' and any logic within it into the load event and things would execute in the order you want.