Skip to content

Instantly share code, notes, and snippets.

@NickIliev
Last active November 22, 2018 13:25
Show Gist options
  • Save NickIliev/5fd7f1c8856c4577c1e30898b97a4990 to your computer and use it in GitHub Desktop.
Save NickIliev/5fd7f1c8856c4577c1e30898b97a4990 to your computer and use it in GitHub Desktop.
Firebase Storage Upload File
import { storage } from "nativescript-plugin-firebase";
import { File, knownFolders, path } from "tns-core-modules/file-system";
import { APPSPOT_BUCKET_URL } from "./shared/link"; // e.g. gs://firebase-storage-demo.appspot.com
uploadFile() {
const appPath = knownFolders.currentApp().path;
// The path to the file we want to upload (this one is in `app/images` and is called `happy-homer-meme.png`)
const logoPath = appPath + "/images/happy-homer-meme.png";
// Upload the file with the options below:
storage.uploadFile({
// optional, can also be passed during init() as 'storageBucket' param so we can cache it (find the URL in the Firebase console)
bucket: APPSPOT_BUCKET_URL,
// the full path of the file in your Firebase storage (folders will automatically be created)
remoteFullPath: 'uploads/images/happy-homer-meme.png',
// option 1: a file-system module File object
localFile: File.fromPath(logoPath),
// option 2: a full file path (ignored if 'localFile' is set)
localFullPath: logoPath,
// get notified of file upload progress
onProgress: status => {
console.log("Uploaded fraction: " + status.fractionCompleted);
console.log("Percentage complete: " + status.percentageCompleted);
}
}).then(uploadedFile => {
console.log("File uploaded: " + JSON.stringify(uploadedFile));
}).catch(err => {
console.log(err);
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment