new File()

This class allows you to interact with files stored in your BC site. Usage examples:

Create a new file.

var f = BCAPI.Models.FileSystem.Root.file('test-file.txt');
var data = 'Hello World !';
f.upload(data).done(function () {
    console.log('File uploaded succesfully');
}).error(function (jqXHR) {
    console.log("Request failed.");
    console.log("Error code: " + jqXHR.status);
    console.log("Error text: " + jqXHR.statusText);
    console.log("Response text: " + jqXHR.responseText);
});

A file is created in your site's file system only after uploading some content.

The content can be any javascript object, including file objects obtained from html upload forms.

BCAPI.Models.FileSystem.Root is the root folder in your site's file structure. You can also create a file object by specifying the file's full path.

var f = new BCAPI.Models.FileSystem.File('/hello_world.txt');

If you omit the / at the beginning it will be added automatically.

So the below is equivalent to the above instantiation:

var f = new BCAPI.Models.FileSystem.File('hello_world.txt');

You can also create a file by specifying the name and the parent folder of the file. The following piece of code creates the file /my/special/file.txt:

var f = new BCAPI.Models.FileSystem.File({
    'parent': new BCAPI.Models.FileSystem.Folder('/my/test/special'),
    'name': 'file.txt'
});

f.upload(f[0]);

In the example above if the parent folders my, test and special do not exist they wull be created on the fly.

Get the file metadata

var f = BCAPI.Models.FileSystem.Root.file('hello_world.txt');
f.fetch().done(function () {
    console.log('File name is: ', f.get('name'));
    console.log('Last update date is: ', f.get('lastModified'));
}).error(function (jqXHR) {
    console.log("Request failed.");
    console.log("Error code: " + jqXHR.status);
    console.log("Error text: " + jqXHR.statusText);
    console.log("Response text: " + jqXHR.responseText);
});

Download the file content

var f = BCAPI.Models.FileSystem.Root.file('new-filea.txt');
f.download().done(function (content) {
    console.log('File content is: ' + content);
}).error(function (jqXHR) {
    console.log("Download failed");
    console.log("Error code: " + jqXHR.status);
    console.log("Error text: " + jqXHR.statusText);
    console.log("Response text: " + jqXHR.responseText);
});

Rename a file

Use save to change the name of a file.

var f = new BCAPI.Models.FileSystem.File('/my/file');
f.set('name', 'new-file');
f.save().done(function () {
    console.log('File name has been changed. Path is ' + f.get('path'))
}).error(function (jqXHR) {
    console.log("Rename failed.");
    console.log("Error code: " + jqXHR.status);
    console.log("Error text: " + jqXHR.statusText);
    console.log("Response text: " + jqXHR.responseText);
})

Delete a file

var f = BCAPI.Models.FileSystem.Root.file('hello_world.txt');
f.destroy().done(function () {
    console.log('File was deleted');
}).error(function (jqXHR) {
    console.log("Delete failed.");
    console.log("Error code: " + jqXHR.status);
    console.log("Error text: " + jqXHR.statusText);
    console.log("Response text: " + jqXHR.responseText);
});

Source:

Methods

contentUrl() → {string}

Returns the url where the content can be accessed.

Source:

Returns

The URL of the resource

Type: string

download() → {promise}

Downloads the content of the file

Source:

Returns

A promise which will be resolved with the content of the file.

Type: Promise

upload(data) → {promise}

Uploads a new content for the file. This method can be called if the file isn't yet created - the file will be created afterwards.

Parameters

Name Type Description
data any

the data object which will be the file's content

Source:

Returns

A promise that will be completed when the file is uploaded

Type: Promise

uploadAndFetch(data) → {promise}

Uploads new content and fetches the metadata for the file which will then be used to populate the object. This method can be called even if the file isn't created yet. Useful if you want to create a file and retrieve it's metadata resulted from the new content immediatly.

Parameters

Name Type Description
data any

The data object

Source:

Returns

A promise that will be completed when the file is uploaded and the new metadata is retrieved.

Type: Promise