saveFile()

The saveFile() function (server-side) creates or updates a file in the File Cabinet.

Syntax

Use this syntax for the saveFile() function:

            saveFile({
    name: string,
    folder: string | number,
    async: true | false
    type: string
    content: string
}).done(callback)
  .fail(callback); 

          

Return Value

When using the saveFile() function in synchronous mode (async: false), the result (success or error data) is returned directly from the saveFile() call itself. You can assign it to a variable and work with it directly.

When using the function in asynchronous mode, it returns a promise, which resolves to an object passed to the callback functions you attach using .done() or .fail().

If the file is saved successfully, the saveFile() function returns an object containing the file's ID and its URL. The returned object has the following structure:

            {
    "id": number,
    "url": string
} 

          

If there's an error, the function returns an object containing an error name, an error code and a description. The returned error object contains the following information:

            {
name: "CPQ-FileCab error",
code: "RCRD_DSNT_EXIST",
message: "That record does not exist. path: path/filename.extension"
} 

          

Parameters

Note:

Required parameters:

  • name

  • folder

  • content

The type parameter is required for custom extensions not included in File Types.

The saveFile() function accepts a single object as a parameter. The object includes the following properties:

  • name - Specifies the name of the file to create, including the extension, as a string. The name doesn't include the path. This is the name format:

                    'filename.extension' 
    
                  
  • folder - Specifies the ID of the folder in which to save the file. You can find the folder ID in the File Cabinet URL after folder=.

  • async - Determines whether the file is saved asynchronously or synchronously. This parameter is true by default.

  • type - Specifies the file type. Because the file type is automatically determined based on the file extension, this parameter is only required for custom extensions. If the file isn't binary, you can use PLAINTEXT as a type for most custom files. For more information about file types, see File Types.

  • content - Specifies the file content to save.

Examples

The following examples show how to use the saveFile() function.

Saving a File Asynchronously

This example saves a text file to a specific folder asynchronously, which is the default behavior.

              saveFile({
    name: 'Notes.txt',
    folder: 2988,
    content: 'This is the file content.'
}).done(function(data) {
    console.log(data);
}); 

            

Saving a File Synchronously

This example saves the file synchronously and paused any further processing until the file is saved. The file creation result is immediately available in the newFile variable.

              newFile = saveFile({
    name: 'Notes.txt',
    folder: 2988,
    content: 'This is the file content.',
    async: false
});
console.log(newFile); 

            

File Types

The table lists the supported file types with their extensions and content types. If a file's extension isn't in this list, you must specify the file type to avoid errors.

File Type Name

File Type ID

Extension

Content Type

AutoCad

AUTOCAD

.dwg

application/x-autocad

BMP Image

BMPIMAGE

.bmp

image/x-xbitmap

CSV File

CSV

.csv

text/csv

Excel File

EXCEL

.xls

application/vnd.ms-excel

Flash Animation

FLASH

.swf

application/x-shockwave-flash

GIF Image

GIFIMAGE

.gif

image/gif

GNU Zip File

GZIP

.gz

application/x-gzip-compressed

HTML File

HTMLDOC

.htm

text/html

Icon Image

ICON

.ico

image/ico

JavaScript File

JAVASCRIPT

.js

text/javascript

JPEG Image

JPGIMAGE

.jpg

image/jpeg

JSON File

JSON

.json

application/json

Message RFC

MESSAGERFC

.eml

message/rfc822

MP3 Audio

MP3

.mp3

audio/mpeg

MPEG Video

MPEGMOVIE

.mpg

video/mpeg

Project File

MSPROJECT

.mpp

application/vnd.ms-project

PDF File

PDF

.pdf

application/pdf

PJPEG Image

PJPGIMAGE

.pjpeg

image/pjpeg

Plain Text File

PLAINTEXT

.txt

text/plain

PNG Image

PNGIMAGE

.png

image/x-png

PostScript File

POSTSCRIPT

.ps

application/postscript

PowerPoint File

POWERPOINT

.ppt

application/vnd.ms-powerpoint

QuickTime Video

QUICKTIME

.mov

video/quicktime

RTF File

RTF

.rtf

application/rtf

SMS File

SMS

.sms

application/sms

CSS File

STYLESHEET

.css

text/css

TIFF Image

TIFFIMAGE

.tiff

image/tiff

Visio File

VISIO

.vsd

application/vnd.visio

Word File

WORD

.doc

application/msword

XML File

XMLDOC

.xml

text/xml

Zip File

ZIP

.zip

application/zip

Related Topics

General Notices