Function to download files to local in nativescript 4.x
const fs = require('file-system'); | |
const httpModule = require("http"); | |
exports.download = function (args) { | |
let data = args.object; | |
context.set("xLoading", { "status": true, "text": "Downloading..." }); | |
// path download path directory in storage | |
var androidDownloadsPath = android.os.Environment.getExternalStoragePublicDirectory(android.os.Environment.DIRECTORY_DOWNLOADS).toString(); | |
// get root path directory in storage | |
var directory = android.os.Environment.getExternalStorageDirectory().getAbsolutePath().toString(); | |
let url = data.get("url"); | |
let filename = url.substring(url.lastIndexOf('/') + 1); | |
let saveLocation = fs.path.join(androidDownloadsPath, filename); | |
httpModule.getFile(url, saveLocation).then(function (file) { | |
context.set("xLoading", { "status": false, "text": "Downloading..." }); | |
alert("Successfully downloaded").then(() => { }); | |
}).catch(function (error) { | |
context.set("xLoading", { "status": false, "text": "Downloading..." }); | |
alert("error occurred!").then(() => { }); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment