Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

Document.getFileAsync method (JavaScript API for Office)

Office Add-ins

Returns the entire document file in slices of up to 4194304 bytes (4MB).

Last modified: June 29, 2015

Note Note

The name "apps for Office" is changing to "Office Add-ins". During the transition, the documentation and the UI of some Office host applications and Visual Studio tools might still use the term "apps for Office". For details, see New name for apps for Office and SharePoint.

Hosts:

Excel, PowerPoint, Word

Available in Requirement set

File

Last changed in File

1.1

See all support details

Office.context.document.getFileAsync(fileType [, options], callback);

Name

Type

Description

Support notes

fileType

FileType

Specifies the format in which the file will be returned. Required.

Host

Supported fileType

Excel Online

Office.FileType.Compressed

PowerPoint on Windows desktop

Office.FileType.Compressed
Office.FileType.Pdf

Word on Windows desktop and iPad

Office.FileType.Compressed
Office.FileType.Pdf
Office.FileType.Text

Word Online

Office.FileType.Compressed
Office.FileType.Pdf

Changed in 1.1, see Support history

options

object

Specifies any of the following optional parameters

sliceSize

number

Specifies the desired slice size (in bytes) up to 4194304 bytes (4MB). If not specified, a default slice size of 4194304 bytes (4MB) will be used.

asyncContext

array, boolean, null, number, object, string, or undefined

A user-defined item of any type that is returned in the AsyncResult object without being altered.

callback

object

A function that is invoked when the callback returns, whose only parameter is of type AsyncResult.

When the function you passed to the callback parameter executes, it receives an AsyncResult object that you can access from the callback function's only parameter.

In the callback function passed to the getFileAsync method, you can use the properties of the AsyncResult object to return the following information.

Property

Use to...

AsyncResult.value

Access the File object.

AsyncResult.status

Determine the success or failure of the operation.

AsyncResult.error

Access an Error object that provides error information if the operation failed.

AsyncResult.asyncContext

Access your user-defined object or value, if you passed one as the asyncContext parameter.

The fileType parameter can be specified using the following enumerations or text values.

FileType enumeration

Enumeration

Value

Description

Office.FileType.Compressed

"compressed"

Returns the entire document (.docx, .pptx , or .xslx) in Office Open XML (OOXML) format as a byte array.

Office.FileType.Pdf

"pdf"

Returns the entire document in PDF format as a byte array.

Office.FileType.Text

"text"

Returns only the text of the document as a string.

No more than two documents are allowed to be in memory; otherwise the getFileAsync operation will fail. Use the File.closeAsync method to close the file when you are finished working with it.

The following example gets the document in 2097152 bytes (2MB) slices as a "pdf" file type.

function getDocumentAsPDF() {
    Office.context.document.getFileAsync("pdf", {sliceSize: 2097152}, function (result) {
        if (result.status == "succeeded") {
            // If the getFileAsync call succeeded, then 
                     // result.value will return a valid File Object.
            var myFile = result.value;
            var sliceCount = myFile.sliceCount;
            var slicesReceived = 0, gotAllSlices = true, docdataSlices = [];
            app.showNotification("File size:" + myFile.size + " #Slices: " + sliceCount);

            // Iterate over the file slices.
            for (var i = 0; i < sliceCount && gotAllSlices; i++)
            {
                myFile.getSliceAsync(i, function (sliceResult) {
                    if (sliceResult.status == "succeeded") {
                        if (!gotAllSlices) { // Failed to get all slices, no need to continue.
                            return;
                        }

                        // Got one slice, store it in a temporary array.
                        // (Or you can do something else, such as 
                        // send it to a third-party server.)
                        docdataSlices[sliceResult.value.index] = sliceResult.value.data;
                        if (++slicesReceived == sliceCount) {
                            // All slices have been received.
                            myFile.closeAsync();
                            onGotAllSlices(docdataSlices);
                        }
                    }
                    else {
                        gotAllSlices = false;
                        myFile.closeAsync();
                        app.showNotification("Error:", result.error.message);
                    }
                });
            }
        }
        else {
            app.showNotification("Error:", result.error.message);
        }
    });
}

function onGotAllSlices(docdataSlices) {
    var docdata = [];
    for (var i = 0; i < docdataSlices.length; i++) {
        docdata = docdata.concat(docdataSlices[i]);
    }

    var fileContent = new String();
    for (var j = 0; j < docdata.length; j++) {
        fileContent += String.fromCharCode(docdata[j]);
    }

    // Now all the file content is stored in 'fileContent' variable, 
    // you can do something with it, such as print, fax...
}

A checkmark (√) in the following matrix indicates that this method is supported in the corresponding Office host application. An empty cell indicates that the Office host application doesn't support this method.

For more information about Office host application and server requirements, see Requirements for running apps for Office.

Supported hosts, by platform

Office for Windows desktop

Office Online
(in browser)

Office for iPad

Excel

PowerPoint

Word

Available in requirement sets

File

Minimum permission level

ReadAllDocument

App types

Content, task pane

Library

Office.js

Namespace

Office

Version

Changes

1.1

In Excel Online, added support for Office.FileType.Compressed as the fileType parameter.

1.1

In Word Online, added support for Office.FileType.Compressed and Office.FileType.Pdf as the fileType parameter.

1.1

In Word on Office for iPad, added support for all FileType values as the fileType parameter.

1.1

In Word and PowerPoint on Windows desktop, added support for Office.FileType.Pdf as the fileType parameter..

1.0

Introduced

Show:
© 2015 Microsoft