UploadFile - JavaScript
Method uploads the supplied file to the brainCloud server. Note that you must call prepareUserUpload to retrieve the uploadId before calling this method. It is assumed that any methods required to monitor the file upload including progress, and completion are attached to the XMLHttpRequest xhr object's events such as:
- xhr.upload.addEventListener("progress", uploadProgress);
- xhr.addEventListener("load", transferComplete);
- xhr.addEventListener("error", transferFailed);
- xhr.addEventListener("abort", transferCanceled);
On a 200 http status, the JSON return is available from XMLHttpRequest.responseText
Service | Operation |
---|---|
file | UPLOAD_FILE |
Method Parameters
Parameter | Description |
---|---|
xhr | The XMLHttpRequest object that the brainCloud client will use to upload the file |
file | The file object |
uploadId | The upload id obtained via prepareFileUpload() |
Usage
http://localhost:3000
- C#
- C++
- Objective-C
- Java
- JavaScript
- Dart
- Cloud Code
- Raw
// N/A
// N/A
// N/A
// N/A
var shareable = true;
var replaceIfExists = true;
var file = document.getElementById('fileToUpload').files[0];
var fileSize = file.size;
_bc.file.prepareFileUpload(
"cloudPath", "cloudFilename", shareable, replaceIfExists, fileSize,
function(result) {
if (result.status == 200) {
var uploadId = result.data.fileDetails.uploadId;
_bc.file.uploadFile(xhr, file, uploadId);
}
}
);
// N/A
// N/A
// N/A
JSON Response
{
"status": 200,
"data": {
"fileDetails": {
"updatedAt": 1452616408147,
"fileSize": 100,
"fileType": "User",
"expiresAt": 1452702808146,
"shareable": true,
"uploadId": "cf9a075c-587e-4bd1-af0b-eab1a79b958f",
"createdAt": 1452616408147,
"profileId": "bf8a1433-62d2-448e-b396-f3dbffff44",
"gameId": "99999",
"path": "dir1/dir2",
"filename": "filename",
"replaceIfExists": true,
"cloudPath": "bc/g/99999/u/bf8a1433-62d2-448e-b396-f3dbffff44/f/dir1/dir2/filename"
}
}
}
Common Error Code
Status Codes
The XMLHttpRequest object will return a 200 http status if the upload is successful.