Giter Site home page Giter Site logo

nativescript-downloadmanager's Introduction

Nativescript DownloadManager

DownloadManager is a library that allows downloading files from Nativescript in Android using the Android Download Manager. Why not use Http.getFile() ? Pretty much because as i see it is pretty much broken if you want to download something thats not just a few kilobytes.

Installing

  1. tns plugin add nativescript-downloadmanager

  2. Make sure the following permissions are in your android manifest

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.INTERNET"/>
    

Usage

Usage is pretty much straightforward: This is a commented example on top of a simplified version of the default Hello world application (aka. just removed the tap count stuff and added a download method).

TLDR: check the download method.

import {Observable} from 'data/observable';
// Import the class.
import {DownloadManager} from 'nativescript-downloadmanager'

export class HelloWorldModel extends Observable {

    private _message: string;

    constructor() {
        super();
    }

    public download() {
    	// Instantiate a Download Manager. The way it's done (it uses a BroadcastReceiver), 
    	// it's mean to be kept alive during all the application lifetime. But we can kill unsubscribe 
        let dm = new DownloadManager();
        // We download a file, in this example a 10mb test file. 
        // This is the Most simple version of doing it.
        // Aside from that there are optional parameters for. Directory (always inside android/data/yourapp/),
        // The file name, and title and description for the notification bar. By default it uses the file name 
        // as title, and no description.
        dm.downloadFile("http://cachefly.cachefly.net/10mb.test", function(result,uri) {
            // result is a boolean, if the download was successful, it will return true
            console.log(result);
            // Uri in file:// format of the downloaded file.
            console.log(uri);
            // unregisterBroadcast is used to unregister the broadcast (For example if you just want to 
            // download a single file).
            dm.unregisterBroadcast();
        })
    }
}

Todo

  • More testing.
  • Document it better.
  • Make some kind of iOS emulatedish version.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.