# File

## AV. File

#### new File(name, data, mimeTypeopt)

Source:
  - file.js, line 95

An AV.File is a local representation of a file that is saved to the AV cloud.

##### Parameters:

Name | Type | Attributes | Description
--- | --- | --- | ---
`name` | String |  | The file's name. This will change to a unique value once the file has finished saving.

`data`

Array

The data for the file, as either: 1. an Array of byte value Numbers, or 2. an Object like { base64: "..." } with a base64-encoded String. 3. a File object selected with a file upload control. (3) only works in Firefox 3.6+, Safari 6.0.2+, Chrome 7+, and IE 10+. 4.a Buffer object in Node.js runtime. For example:

    var fileUploadControl = $("#profilePhotoFileUpload")[0];
    if (fileUploadControl.files.length > 0) {
      var file = fileUploadControl.files[0];
      var name = "photo.jpg";
      var file = new AV.File(name, file);
      file.save().then(function() {
        // The file has been saved to AV.
      }, function(error) {
        // The file either could not be read, or could not be saved to AV.
      });
    }

`mimeType` | String | <optional> | Content-Type header to use for the file. If this is omitted, the content type will be inferred from the name's extension.

### Methods

#### (static) createWithoutData(objectId) → {AV.File}

Source:
  - file.js, line 169

Creates a file object with exists objectId.

##### Parameters:

Name | Type | Description
--- | --- | ---
`objectId` | String | The objectId string

##### Returns:

the file object

Type
  AV.File

#### (static) withURL(name, url, metaDataopt, typeopt) → {AV.File}

Source:
  - file.js, line 146

Creates a fresh AV.File object with exists url for saving to AVOS Cloud.

##### Parameters:

Name | Type | Attributes | Description
--- | --- | --- | ---
`name` | String |  | the file name
`url` | String |  | the file url.
`metaData` | Object | <optional> | the file metadata object.
`type` | String | <optional> | Content-Type header to use for the file. If this is omitted, the content type will be inferred from the name's extension.

##### Returns:

the file object

Type
  AV.File

#### destroy(options) → {Promise}

Source:
  - file.js, line 377

Destroy the file.

##### Parameters:

Name | Type | Description
--- | --- | ---
`options` | AuthOptions |

##### Returns:

A promise that is fulfilled when the destroy completes.

Type
  Promise

#### fetch(fetchOptions, options) → {Promise}

Source:
  - file.js, line 530

fetch the file from server. If the server's representation of the model differs from its current attributes, they will be overriden,

##### Parameters:

Name | Type | Description
--- | --- | ---
`fetchOptions` | Object | Optional options to set 'keys', 'include' and 'includeACL' option.
`options` | AuthOptions |

##### Returns:

A promise that is fulfilled when the fetch completes.

Type
  Promise

#### get(The) → {Any}

Source:
  - file.js, line 251

Gets the attributs of the file object.

##### Parameters:

Name | Type | Description
--- | --- | ---
`The` | String | attribute name which want to get.

##### Returns:

Type
  Any

#### getACL() → {AV.ACL}

Source:
  - file.js, line 213

Returns the ACL for this file.

##### Returns:

An instance of AV.ACL.

Type
  AV.ACL

#### metaData(attr, value) → {Object}

Source:
  - file.js, line 316

Returns the file's metadata JSON object if no arguments is given.Returns the metadata value if a key is given.Set metadata value if key and value are both given.

     var metadata = file.metaData(); //Get metadata JSON object.
     var size = file.metaData('size');  // Get the size metadata value.
     file.metaData('format', 'jpeg'); //set metadata attribute and value.

##### Parameters:

Name | Type | Description
--- | --- | ---
`attr` | String | an optional metadata key.
`value` | Object | an optional metadata value.

##### Returns:

The file's metadata JSON object.

Type
  Object

#### name()

Source:
  - file.js, line 233

Gets the name of the file. Before save is called, this is the filename given by the user. After save is called, that name gets prefixed with a unique identifier.

#### ownerId() → {String}

Source:
  - file.js, line 367

Returns the file's owner.

##### Returns:

The file's owner id.

Type
  String

#### save(optionsopt) → {Promise}

Source:
  - file.js, line 422

Saves the file to the AV cloud.

##### Parameters:

Name | Type | Attributes | Description
--- | --- | --- | ---

`options`

Object

<optional>

###### Properties

Name | Type | Attributes | Description
--- | --- | --- | ---
`onprogress` | UploadProgressCallback | <optional> | 文件上传进度，在 Node.js 与小程序中无效，回调参数说明详见 UploadProgressCallback。

##### Returns:

Promise that is resolved when the save finishes.

Type
  Promise

#### set(Object, attr, value) → {String|Number|Array|Object}

Source:
  - file.js, line 274

Set the metaData of the file object.

##### Parameters:

Name | Type | Description
--- | --- | ---
`Object` | Object | is an key value Object for setting metaData.
`attr` | String | is an optional metadata key.
`value` | Object | is an optional metadata value.

##### Returns:

Type
  String | Number | Array | Object

#### setACL(acl)

Source:
  - file.js, line 221

Sets the ACL to be used for this file.

##### Parameters:

Name | Type | Description
--- | --- | ---
`acl` | AV.ACL | An instance of AV.ACL.

#### size() → {Number}

Source:
  - file.js, line 359

Returns the file's size.

##### Returns:

The file's size in bytes.

Type
  Number

#### thumbnailURL(width, heigth, quality, scaleToFit, fmt) → {String}

Source:
  - file.js, line 337

如果文件是图片，获取图片的缩略图URL。可以传入宽度、高度、质量、格式等参数。

##### Parameters:

Name | Type | Description
--- | --- | ---
`width` | Number | 宽度，单位：像素
`heigth` | Number | 高度，单位：像素
`quality` | Number | 质量，1-100的数字，默认100
`scaleToFit` | Number | 是否将图片自适应大小。默认为true。
`fmt` | String | 格式，默认为png，也可以为jpeg,gif等格式。

##### Returns:

缩略图URL

Type
  String

#### url() → {String}

Source:
  - file.js, line 242

Gets the url of the file. It is only available after you save the file or after you get the file from a AV.Object.

##### Returns:

Type
  String