Document.getFileAsync method (JavaScript API for Office v1.1)

JavaScript API for Office v1.1

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

Last modified: June 12, 2014

Introduced in Office.js version

1.0

Updated in Office.js version

1.1

Available in requirement sets

File

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

fileType

Type: FileType

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

options

Type: object

Specifies the following optional parameters.

sliceSize

Type: 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. Optional.

asyncContext

Type: 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. Optional.

callback

Type: 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. PowerPoint 2013 supports only the "compressed" file type. Word 2013 supports both "compressed" and "text" file types.

FileType enumeration

Enumeration

Value

Description

Office.FileType.Compressed

"compressed"

Returns the entire document (.pptx or .docx) 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. (Word only)

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.

  • In apps for Word and PowerPoint, you can now get the entire document in PDF format.

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

var i = 0;
var slices = 0;

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.
             myFile = result.value;
             slices = myFile.sliceCount;
             document.getElementById("result").innerText = " File size:" + myFile.size + " #Slices: " + slices;
             
             // Iterate over the file slices.
             for ( i = 0; i < slices; i++) {
                 var slice = myFile.getSliceAsync(i, function (result) {
                     if (result.status == "succeeded") {  
                         doSomethingWithChunk(result.value.data);
                         if (slices == i) // Means it's done traversing...
                         {
                             SendFileComplete();
                         }
                     }
                     else
                         document.getElementById("result").innerText = result.error.message;
                     });
             }
             myFile.closeAsync();
        }
        else
            document.getElementById("result2").innerText = result.error.message;
    });
}


App types

Content apps, Task pane apps

Supported clients

PowerPoint 2013, PowerPoint 2013 SP1, PowerPoint 2013 RT, Word 2013, Word 2013 SP1, Word 2013 RT

Library

Office.js

Namespace

Office

Show:
© 2014 Microsoft