Last active
August 7, 2019 04:52
-
-
Save dyazincahya/8d4fce09f5249565147d85fe4ef6ee0c to your computer and use it in GitHub Desktop.
Function to download files to local in nativescript 4.x
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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