BasicsJavaScript

Overview

Download the MediaFire JavaScript SDK

Description: MediaFire's JavaScript SDK enables your application to take advantage of MediaFire's REST API through convenient AJAX calls.

In each section of this guide, you may see colored boxes which are meant to highlight important information:

Blue boxes will call your attention to important information we don't want you to miss.


Getting Started


The MediaFire SDK is composed of several components, all of which are optional. They are:

mediafire.js: The MediaFire SDK. Contains the API wrappers and session manager.
auth.js: An optional script that augments the SDK login function to provide an Auth-like login dialog. It can also be used without the SDK.
mfuploader.js: Provides upload functionality to the SDK. This script is optional. The script is loaded asynchronously when first calling the SDK upload function.
hasher.js: A web worker that handles file hashing for mfuploader.js. This script is required for the upload functionality to work. See hasher.pexe for the exception.
sha256.js: A SHA-256 library loaded by the hasher.js web worker. The file is only required when using hasher.js.
Optional

hasher.nmf: The PNaCl manifest, it is required if you intend to use hasher.pexe, which is optional.
hasher.pexe: This PNaCl executable is optional and interchangeable with hasher.js. Currently, PNaCl is only supported in Google Chrome 31+. If it's not supported, the uploader will fallback to hasher.js.


Installation


Load the SDK using the <script> element. If you wish to use the auth-like login dialog, you must include 'auth.js'. Initialize an instance of the SDK with your application ID.


The appID needed is in the Developer application ID which can be obtained from your MediaFire account. See the Getting Started guide for third-party developers.

Example
<script src="mediafire.js"></script>
<script src="auth.js"></script>
<script>
    var app = new MF(appID, options);
    ...
</script>

PNaCl Module
If you wish to use the optional PNaCl module, you must serve the manifest (hasher.nmf) with the following mime-type: application/x-pnacl, and you must include a listener element in your html. The PNaCl is an optional replacement for the webworker hasher. It speeds up file hashing greatly on supported browsers. It is currently only supported on Chrome 31 and higher.



Example
`<div id="pnacl_listener"></div>`

Using Connector Framework


Constructor: To begin using the connected frameworks initialize an instance of the MediaFire object:

Required Parameter:

  • appID (integer): This is your MediaFire application ID. It will be represented by a number.

Optional Parameter:
  • options (object): Custom property values to override the default API SDK properties.

The appID needed is in the application ID which can be obtained from the Developers section of your MediaFire account. See the Getting Started guide for third-party developers.


Example
var app = new MF(9001);

Login


Description: This will create a new user session. The syntax is as follows:

app.login(credentials, callback);


Required Parameter:
  • credentials (object): The email address and password associated with the account you would like to access. This should be in the form of a JavaScript object with the properties email and password.

Optional Parameter:
  • callback (function or object): Either a single function to be executed upon completion of the request or a JavaScript object containing a success function and/or an error function.


Example
app.login({
    email: 'michael.bolton@initech.com',
    password: 'pcloadletter'
});


Login Button



BUTTON CODE
Text copied to clipboard!
Copy Code


API


Description: Sends an asynchronous API request with optional response callbacks. See the REST API documentation for a detailed listing of available methods and their use.

Required Parameter:

  • path (string): The relative API path (e.g., "user/get_info").

Optional Parameters:
  • options (object): Parameters to include with the request.
  • callback (function or object): Either a single function to be executed upon completion of the request or a JavaScript object containing a success function and/or an error function.

Example
app.api('user/get_info', null, function(data) {
    alert(data.response);
});
Example
var contentOptions = {
    content_type: 'files',
    filter: 'video',
    order_by: 'created',
    order_direction: 'asc',
    chunk: 1
};

app.api('folder/get_content', contentOptions, function(data) {
    alert('Video count in root folder: ' + data.response.folder_content.files.length);
});
Example
var fileDetails = {
    filename: 'example.txt'
};

app.api('file/create', fileDetails, function(data) {
    alert('New file quickkey: ' + data.response.fileinfo.quickkey);
});

Upload


Description: Uploads files to the user's account.

Required Parameters:

  • files (object): FileList object from an event.

Optional Parameters:
  • callback (function or object): Either a single function to be executed upon completion of the request or a JavaScript object containing a success function and/or an error function.
  • options (object): Configurations specific to the uploader.

Configuration:
  • Callbacks: Callbacks are configured by assigning functions to the optional callback parameter.
    • onUpdate (function): This callback will be fired anytime a file changes state, useful for modal updates.
    • onUploadProgress (function): This callback will indicate which file and how many bytes have been uploaded.
    • onHashProgress (function): This callback will indicate which file and how many bytes have been hashed.
    • onDuplicateConfirm (function): This callback provides an opportunity to choose what to do when a duplicate file is encountered in the cloud during upload.

  • Configurations: Upload settings are configured by assigning properties to the optionaloptions parameter.
    • folderkey (string): This is the destination for the upload. If not specified, the My Files root will be the target.
    • resourcePath (string): Allows you to specify a relative path to the uploader resources (hasher.nmf, hasher.pexe, hasher.js) by default it is the same path as the SDK.
    • concurrentUploads (integer): This configures the number of files that the uploader will attempt to upload to the cloud concurrently.
    • retryAttempts (integer): This configures the number of times the uploader will attempt to retry uploading files to the cloud.
    • disableInstantUploads (true/false): This configurations property determines whether or not the uploader will attempt to upload a file entirely by hash matching with the cloud.
    • actionOnDuplicate (string): Setting this configuration property will determine what the default action will be when a duplicate file name exists in the target folder. Possible values are "skip" (default), "keep" (which appends a number to the new file), and "replace".
    • returnThumbnails (true/false): Whether to generate thumbnails and include them in the file object (file.dataURL).
    • filterByExtension (string or array): Allows you to whitelist specific extensions, excluding all others.

Example
document.getElementById('files').onchange = function(e) {
    app.upload(e.target.files, {
        onUpdate: function(uploader, file, state) {
            console.log(file.name, state);
        },

        onUploadProgress: function(uploader, file, bytes) {
            console.log('uploaded: ', bytes);
        },

        onHashProgress: function(uploader, file, bytes) {
            console.log('hashed: ', bytes);
        },

        onDuplicateConfirm: function(uploader, file) {
            var choice = prompt('Duplicate found: keep, skip, or replace?');
            var applyAll = false;

            if(uploader.files.length > 1) {
                applyAll = confirm('Apply "' + choice + '" to all?');
            }

            uploader.duplicateAction(file, choice, applyAll);
        }
    });
};