Instantly share code, notes, and snippets.

Embed
What would you like to do?
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..." });
var androidDownloadsPath = android.os.Environment.getExternalStoragePublicDirectory(android.os.Environment.DIRECTORY_DOWNLOADS).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