Skip to content

Instantly share code, notes, and snippets.

@andygup
Created February 9, 2018 02:07
Show Gist options
  • Select an option

  • Save andygup/7c6d4ccf9dbae9181a3db57b16bdb929 to your computer and use it in GitHub Desktop.

Select an option

Save andygup/7c6d4ccf9dbae9181a3db57b16bdb929 to your computer and use it in GitHub Desktop.
Example csv parser using FileReader
this._loadFromFile = function(file,store,callback){
if (window.File && window.FileReader && window.FileList && window.Blob)
{
// Great success! All the File APIs are supported.
var reader = new FileReader();
reader.onload = function(evt)
{
var csvContent = evt.target.result;
var tiles = csvContent.split("\r\n");
var tileCount = 0;
var pair, tile;
if(tiles[0] !== "url,img")
{
return callback(false, "File " + file.name + " doesn't contain tiles that can be loaded");
}
for(var i=1; i<tiles.length; i++)
{
pair = tiles[i].split(",");
tile = {
url: pair[0],
img: pair[1]
};
console.log("read",tile.url);
store.store(tile,function(success)
{
console.log(".");
if( success )
{
tileCount += 1;
}
if( tileCount === tiles.length-1)
{
console.log("finished!");
window.setTimeout(function() { /* refresh layer by zooming in and out, or some way that really refreshes the layer */ }, 1000);
callback(true, tileCount + " tiles loaded from " + file.name);
}
});
}
};
reader.readAsText(file);
}
else
{
callback(false, "The File APIs are not fully supported in this browser.");
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment