nodeFileSystem = require('fs'); API.downloadItrData(newBook.isbn, newBook.version).then(function(response) { if (response.status === 200 && response.data.success) { var zipLocation = response.data.data.zipLocation; var zipSaveLocation = Constants.PATH_BASE + Constants.PATH_ITR_FOLDER + newBook.isbn + "/" + newBook.version + ".zip"; $http.get(zipLocation, { responseType: "blob" } ).then(function(zipResult) { var reader = new FileReader(); reader.addEventListener("loadend", function() { nodeFileSystem.writeFile(zipSaveLocation, toBuffer(reader.result)); }); reader.readAsArrayBuffer(zipResult.data); }); } });
Ok, but what does the code do? Keep in mind this is just a stub, so this is more of a non-working example rather than code that can be copied and pasted into your work.
First, using an API, I check to see if my book (basically, an ebook) has an update. If it does, I take from the returned JSON to the location of the zip file (zipLocation), which I am going to save to the server (zipSaveLocation).
I then use the angular function $http.get() to download the zip. By setting the responseType in the config I can get the zip file as a "blob" instead of a string.
Once it has downloaded (the promise function .then fires) I use the HTML5 FileReader class to convert the JS Blob to a JS arrayBuffer so it can be converted to a Node.js Buffer so the node filesystem can save it.
I really hope this saves someone the 6 hours or so it took me to figure out :)