|
44 | 44 | * Router Helpers |
45 | 45 | * [`$waitForSignIn()`](#waitforsignin) |
46 | 46 | * [`$requireSignIn(requireEmailVerification)`](#requiresigninrequireemailverification) |
| 47 | +* [`$firebaseStorage`](#firebasestorage) |
| 48 | + * [`$put(file, metadata)`](#putfile-metadata) |
| 49 | + * [`$putString(string, format, metadata)`](#putstringstring-format-metadata) |
| 50 | + * [`$getDownloadURL()`](#getdownloadurl) |
| 51 | + * [`$getMetadata()`](#getmetadata) |
| 52 | + * [`$updateMetadata(metadata)`](#updatemetadatametadata) |
| 53 | + * [`$delete()`](#delete) |
| 54 | + * [`$toString()`](#tostring) |
| 55 | + * [Upload Task](#upload-task) |
| 56 | + * [`$progress(callback)`](#progresscallback) |
| 57 | + * [`$complete(callback)`](#completecallback) |
| 58 | + * [`$error(callback)`](#errorcallback) |
| 59 | + * [`$cancel()`](#cancel) |
| 60 | + * [`$pause()`](#pause) |
| 61 | + * [`$snapshot()`](#snapshot) |
| 62 | + * [`then(callback)`](#then) |
| 63 | + * [`catch(callback)`](#catch) |
47 | 64 | * [Extending the Services](#extending-the-services) |
48 | 65 | * [Extending `$firebaseObject`](#extending-firebaseobject) |
49 | 66 | * [Extending `$firebaseArray`](#extending-firebasearray) |
@@ -966,6 +983,196 @@ users from seeing authenticated pages momentarily during page load. See the |
966 | 983 | ["Using Authentication with Routers"](/docs/guide/user-auth.md#authenticating-with-routers) |
967 | 984 | section of our AngularFire guide for more information and a full example. |
968 | 985 |
|
| 986 | +## $firebaseStorage |
| 987 | + |
| 988 | +AngularFire includes support for [binary storage](/docs/guide/uploading-downloading-binary-content.md) |
| 989 | +with the `$firebaseStorage` service. |
| 990 | + |
| 991 | +The `$firebaseStorage` service takes a [Firebase Storage](https://firebase.google.com/docs/storage/) reference. |
| 992 | + |
| 993 | +```js |
| 994 | +app.controller("MyCtrl", ["$scope", "$firebaseStorage", |
| 995 | + function($scope, $firebaseStorage) { |
| 996 | + var storageRef = firebase.storage().ref("images/dog"); |
| 997 | + $scope.storage = $firebaseStorage(storageRef); |
| 998 | + } |
| 999 | +]); |
| 1000 | +``` |
| 1001 | + |
| 1002 | +The storage object returned by `$firebaseStorage` contains several methods for uploading and |
| 1003 | +downloading binary content, as well as managing the content's metadata. |
| 1004 | + |
| 1005 | +### $put(file, metadata) |
| 1006 | + |
| 1007 | +[Uploads a `Blob` object](https://firebase.google.com/docs/storage/web/upload-files) to the specified storage reference's path with an optional metadata parameter. |
| 1008 | +Returns an [`UploadTask`](#upload-task) wrapped by AngularFire. |
| 1009 | + |
| 1010 | + |
| 1011 | +```js |
| 1012 | +var htmlFile = new Blob(["<html></html>"], { type : "text/html" }); |
| 1013 | +var uploadTask = $scope.storage.$put(htmlFile, { contentType: "text/html" }); |
| 1014 | +``` |
| 1015 | + |
| 1016 | +### $putString(string, format, metadata) |
| 1017 | + |
| 1018 | +[Uploads a raw, `base64` string, or `base64url` string](https://firebase.google.com/docs/storage/web/upload-files#upload_from_a_string) to the specified storage reference's path with an optional metadata parameter. |
| 1019 | +Returns an [`UploadTask`](#upload-task) wrapped by AngularFire. |
| 1020 | + |
| 1021 | +```js |
| 1022 | +var base64String = "5b6p5Y+344GX44G+44GX44Gf77yB44GK44KB44Gn44Go44GG77yB"; |
| 1023 | +// Note: valid values for format are "raw", "base64", "base64url", and "data_url". |
| 1024 | +var uploadTask = $scope.storage.$putString(base64String, "base64", { contentType: "image/gif" }); |
| 1025 | +``` |
| 1026 | + |
| 1027 | +### $getDownloadURL() |
| 1028 | + |
| 1029 | +Returns a promise fulfilled with [the download URL](https://firebase.google.com/docs/storage/web/download-files#download_data_via_url) for the file stored at the configured path. |
| 1030 | + |
| 1031 | +```js |
| 1032 | +$scope.storage.$getDownloadURL().then(function(url) { |
| 1033 | + $scope.url = url; |
| 1034 | +}); |
| 1035 | +``` |
| 1036 | + |
| 1037 | +### $getMetadata() |
| 1038 | + |
| 1039 | +Returns a promise fulfilled with [the metadata of the file](https://firebase.google.com/docs/storage/web/file-metadata#get_file_metadata) stored at the configured path. File |
| 1040 | +metadata contains common properties such as `name`, `size`, and `contentType` |
| 1041 | +(often referred to as MIME type) in addition to some less common ones like `contentDisposition` and `timeCreated`. |
| 1042 | + |
| 1043 | +```js |
| 1044 | +$scope.storage.$getMetadata().then(function(metadata) { |
| 1045 | + $scope.metadata = metadata; |
| 1046 | +}); |
| 1047 | +``` |
| 1048 | + |
| 1049 | +### $updateMetadata(metadata) |
| 1050 | + |
| 1051 | +[Updates the metadata of the file](https://firebase.google.com/docs/storage/web/file-metadata#update_file_metadata) stored at the configured path. |
| 1052 | +Returns a promise fulfilled with the updated metadata. |
| 1053 | + |
| 1054 | +```js |
| 1055 | +var updateData = { contenType: "text/plain" }; |
| 1056 | +$scope.storage.$updateMetadata(updateData).then(function(updatedMetadata) { |
| 1057 | + $scope.updatedMetadata = updatedMetadata; |
| 1058 | +}); |
| 1059 | +``` |
| 1060 | + |
| 1061 | +### $delete() |
| 1062 | + |
| 1063 | +Permanently [deletes the file stored](https://firebase.google.com/docs/storage/web/delete-files) at the configured path. Returns a promise that is resolved when the delete completes. |
| 1064 | + |
| 1065 | +```js |
| 1066 | +$scope.storage.$delete().then(function() { |
| 1067 | + console.log("successfully deleted!"); |
| 1068 | +}); |
| 1069 | +``` |
| 1070 | + |
| 1071 | +### $toString() |
| 1072 | + |
| 1073 | +Returns a [string version of the bucket path](https://firebase.google.com/docs/reference/js/firebase.storage.Reference#toString) stored as a `gs://` scheme. |
| 1074 | + |
| 1075 | +```js |
| 1076 | +// gs://<bucket>/<path>/<to>/<object> |
| 1077 | +var asString = $scope.storage.$toString(); |
| 1078 | +``` |
| 1079 | + |
| 1080 | +### Upload Task |
| 1081 | + |
| 1082 | +The [`$firebaseStorage()`](#firebasestorage) service returns an AngularFire wrapped [`UploadTask`](https://firebase.google.com/docs/reference/js/firebase.storage#uploadtask) when uploading binary content |
| 1083 | +using the [`$put()`](#putfile-metadata) and [`$putString()`](#putstringstring-format-metadata) methods. This task is used for [monitoring](https://firebase.google.com/docs/storage/web/upload-files#monitor_upload_progress) |
| 1084 | +and [managing](https://firebase.google.com/docs/storage/web/upload-files#manage_uploads) uploads. |
| 1085 | + |
| 1086 | +```js |
| 1087 | +var htmlFile = new Blob(["<html></html>"], { type : "text/html" }); |
| 1088 | +var uploadTask = $scope.storage.$put(htmlFile, { contentType: "text/html" }); |
| 1089 | +``` |
| 1090 | + |
| 1091 | +#### $progress(callback) |
| 1092 | + |
| 1093 | +Calls the provided callback function whenever there is an update in the progress of the file uploading. The callback |
| 1094 | +passes back an [`UploadTaskSnapshot`](https://firebase.google.com/docs/reference/js/firebase.storage.UploadTaskSnapshot). |
| 1095 | + |
| 1096 | +```js |
| 1097 | +var uploadTask = $scope.storage.$put(file); |
| 1098 | +uploadTask.$progress(function(snapshot) { |
| 1099 | + var percentUploaded = (snapshot.bytesTransferred / snapshot.totalBytes) * 100; |
| 1100 | + console.log(percentUploaded); |
| 1101 | +}); |
| 1102 | +``` |
| 1103 | + |
| 1104 | +#### $complete(callback) |
| 1105 | + |
| 1106 | +Calls the provided callback function when the upload is complete. Passes back the completed [`UploadTaskSnapshot`](https://firebase.google.com/docs/reference/js/firebase.storage.UploadTaskSnapshot). |
| 1107 | + |
| 1108 | +```js |
| 1109 | +var uploadTask = $scope.storage.$put(file); |
| 1110 | +uploadTask.$complete(function(snapshot) { |
| 1111 | + console.log(snapshot.downloadURL); |
| 1112 | +}); |
| 1113 | +``` |
| 1114 | + |
| 1115 | +#### $error(callback) |
| 1116 | + |
| 1117 | +Calls the provided callback function when there is an error uploading the file. |
| 1118 | + |
| 1119 | +```js |
| 1120 | +var uploadTask = $scope.storage.$put(file); |
| 1121 | +uploadTask.$error(function(error) { |
| 1122 | + console.error(error); |
| 1123 | +}); |
| 1124 | +``` |
| 1125 | + |
| 1126 | +#### $cancel() |
| 1127 | + |
| 1128 | +[Cancels](https://firebase.google.com/docs/reference/js/firebase.storage.UploadTask#cancel) the current upload. |
| 1129 | +Has no effect on a completed upload. Returns `true` if cancel had effect. |
| 1130 | + |
| 1131 | +```js |
| 1132 | +var uploadTask = $scope.storage.$put(file); |
| 1133 | +var hadEffect = uploadTask.$cancel(); |
| 1134 | +``` |
| 1135 | + |
| 1136 | +#### $pause() |
| 1137 | + |
| 1138 | +[Pauses](https://firebase.google.com/docs/reference/js/firebase.storage.UploadTask#pause) the current upload. |
| 1139 | +Has no effect on a completed upload. Returns `true` if pause had effect. |
| 1140 | + |
| 1141 | +```js |
| 1142 | +var uploadTask = $scope.storage.$put(file); |
| 1143 | +var hadEffect = uploadTask.$pause(); |
| 1144 | +``` |
| 1145 | + |
| 1146 | +#### $snapshot() |
| 1147 | + |
| 1148 | +Returns the [current immutable view of the task](https://firebase.google.com/docs/reference/js/firebase.storage.UploadTaskSnapshot) at the time the event occurred. |
| 1149 | + |
| 1150 | +```js |
| 1151 | +var uploadTask = $scope.storage.$put(file); |
| 1152 | +$scope.bytesTransferred = uploadTask.$snapshot.bytesTransferred; |
| 1153 | +``` |
| 1154 | + |
| 1155 | +#### then() |
| 1156 | +An `UploadTask` implements a promise like interface. The callback is called when the upload is complete. The callback |
| 1157 | +passes back an [UploadTaskSnapshot](https://firebase.google.com/docs/reference/js/firebase.storage.UploadTaskSnapshot). |
| 1158 | + |
| 1159 | +```js |
| 1160 | +var uploadTask = $scope.storage.$put(file); |
| 1161 | +uploadTask.then(function(snapshot) { |
| 1162 | + console.log(snapshot.downloadURL); |
| 1163 | +}); |
| 1164 | +``` |
| 1165 | + |
| 1166 | +#### catch() |
| 1167 | +An `UploadTask` implements a promise like interface. The callback is called when an error occurs. |
| 1168 | + |
| 1169 | +```js |
| 1170 | +var uploadTask = $scope.storage.$put(file); |
| 1171 | +uploadTask.catch(function(error) { |
| 1172 | + console.error(error); |
| 1173 | +}); |
| 1174 | +``` |
| 1175 | + |
969 | 1176 | ## Extending the Services |
970 | 1177 |
|
971 | 1178 | There are several powerful techniques for transforming the data downloaded and saved |
|
0 commit comments