Skip to content

Instantly share code, notes, and snippets.

@dyazincahya
Last active August 7, 2019 04:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dyazincahya/8d4fce09f5249565147d85fe4ef6ee0c to your computer and use it in GitHub Desktop.
Save dyazincahya/8d4fce09f5249565147d85fe4ef6ee0c to your computer and use it in GitHub Desktop.
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