Skip to content

How to download a remote file in FuseTools?

Last updated on November 20, 2017

Today I am gonna write about file downloading from a remote source in you FuseTools based mobile applications.

About FuseTools

FuseTools makes apps development faster, easier and more fun for both designers and developers,to now more about fuse please click here.

Fuse Download Files

Downloading files from remote locations is not an easy task in mobile devices, but with fuseTools FileSystem, Environment libraries we can easily achieve it.
Here I am gonna show you, how you can use those libraries to download a remote file into the device. Below is the function which will download the files and place it the os specific location in the devices.

var Environment = require('FuseJS/Environment');
var FileSystem = require("FuseJS/FileSystem");
function ShareHandler() {
    let imageUrl = '';
    var fileName = imageUrl.split('/').pop(); // extract file name from the url
    var oReq = new XMLHttpRequest();
    oReq.open("GET", imageUrl, true);
    oReq.responseType = "arraybuffer";
    oReq.onload = function (oEvent) {
        var arrayBuffer = oReq.response;
        if (arrayBuffer) {
            if (Environment.ios) {
                let devicePath = FileSystem.iosPaths.temp;
            } else (Environment.android) {
                let devicePath = FileSystem.androidPaths.externalCache;
            } 
            var path = devicePath + fileName;
            FileSystem.writeBufferToFile(path, arrayBuffer) .then(function() {
                console.log("Successful write");
            }, function(error) {
                console.log(error);
            });
        }

    };
    oReq.send(null);
}
0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments