WL.backgroundDownload (Windows Store app)

WL.backgroundDownload (Windows Store app)

Last modified: March 13, 2015

Makes a call to download a file from Microsoft OneDrive.

Important note Important

WL.backgroundDownload is supported only for use with Windows Store apps using JavaScript. If you are writing a web app, use WL.download instead.

Parameters

  • properties

    Required. A JSON object that contains the following properties, which are necessary to make the REST API call:

    Name

    Type

    Description

    Default Value

    path

    string

    Required. The path to the file to download. For information on specifying paths for REST objects, see REST reference.

    None

    file_output

    Windows.Storage.StorageFile

    Optional. The file output object to which the downloaded file data is written.

    None

  • callback

    Optional. Specifies a callback function that is executed when the REST API call is complete. The callback function takes the API response object as a parameter. The response object exposes the data that is returned from the Live SDK, or, if an error occurs, an error property that contains the error code.

    Note Note

    Although the callback parameter is still supported, we recommend that you use the Promise object instead, which is described later in this topic.

Return value

Returns a Promise object. This object's then method accepts callback functions for onSuccess, onError, and onProgress to enable your code to handle a successful, failed, and in-progress call to the corresponding WL.download method, respectively.

The onSuccess callback is passed a response object that contains content_type and stream properties, representing the downloaded file's content type and file stream, respectively.

Remarks

Important note Important

Before calling the WL.backgroundDownload function, your code must call either the WL.init or WL.login function with, at minimum, the required scopes for downloading the corresponding files. If you don't do this, the call to the WL.backgroundDownload function may fail. Also, this requirement applies to each webpage's current session. Therefore, if your app contains multiple webpages, each webpage must follow this requirement.

Example

function downloadFile_onClick() {
    var picker = setupSavePicker();
    picker.pickSaveFileAsync().then(
        function (file) {
            if (file && (file instanceof Windows.Storage.StorageFile)) {
                WL.login({
                    scope: "wl.skydrive"
                }).then(
                    function (response) {
                        WL.backgroundDownload({
                            path: "file.8c8ce076ca27823f.8C8CE076CA27823F!129/picture?type=thumbnail",
                            file_output: file
                        }).then(
                            function (response) {
                                document.getElementById("infoLabel").innerText = "Downloaded file.";
                            },
                            function (responseFailed) {
                                document.getElementById("infoLabel").innerText =
                                "Error calling API: " + responseFailed.error.message;
                            }
                        );
                    },
                    function (responseFailed) {
                        document.getElementById("infoLabel").innerText =
                            "Error signing in: " + responseFailed.error.message;
                    }
                );
            }
            else {
                document.getElementById("infoLabel").innerText = "Cannot download file.";
            }
        },
        function (fileFailed) {
            document.getElementById("infoLabel").innerText = "Cannot download file.";
        }
    );
}

function setupSavePicker() {
    var savepicker = new Windows.Storage.Pickers.FileSavePicker();
    savepicker.suggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.documentsLibrary;
    savepicker.fileTypeChoices.insert("Picture", [".jpg"]);
    return savepicker;
}

Requirements

Library

Wl.js

Show:
© 2016 Microsoft