简体   繁体   English

Firebase使用Angular2通过Firebase数据库(Firebase存储)上传文件和图像

[英]Firebase upload files, images over Firebase database (Firebase storage) using Angular2

I am looking for an example for image file upload over Firebase database using Angular2. 我正在寻找使用Angular2在Firebase数据库上传图像文件的示例。

Please provide code sample or any link.. 请提供代码示例或任何链接..

I'm not aware of any Angular2 libraries that can do Firebase Storage. 我不知道任何可以执行Firebase存储的Angular2库。 The AngularFire2 library has a issue open on added the feature. AngularFire2库在添加该功能时打开了一个问题

Follow the instructions from the following link and you can upload file from angular 2 to firebase storage. 按照以下链接中的说明操作,您可以将文件从角度2上传到firebase存储。

https://github.com/Ohtsu/o2-upload-to-fbs https://github.com/Ohtsu/o2-upload-to-fbs

Here are the following necessary steps you can do. 您可以执行以下必要步骤。

Step:1 => Install npm package 步骤:1 =>安装npm包

$ npm install o2-upload-to-fbs --save $ npm install o2-upload-to-fbs --save

Step:2 => Login to your firebase account and create a bucket in the storage section 步骤:2 =>登录您的firebase帐户并在存储部分创建一个存储桶

Step:3 => Now in your app.module.ts file, add the following code 步骤:3 =>现在在app.module.ts文件中,添加以下代码

import { AngularFireModule } from 'angularfire2';       
import { O2UploadToFbsComponent } from 'o2-upload-to-fbs';  

export const firebaseConfig = {
  apiKey: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
  authDomain: "xxxxxxxxxxxxxxxxxx.firebaseapp.com",
  databaseURL: "https://xxxxxxxxxxxxxxxxxx.firebaseio.com",
  storageBucket: "xxxxxxxxxxxxxxxxxx.appspot.com",
  messagingSenderId: "xxxxxxxxxxxxxxxxxx"

};

@NgModule({
 declarations: [
 O2UploadToFbsComponent,  // <= Add
 AppComponent
],
imports: [
  BrowserModule,
  FormsModule,
  HttpModule,
  AngularFireModule.initializeApp(firebaseConfig)  // <= Add

],
  providers: [],
  bootstrap: [AppComponent]
})

and from your firebase account click overview link and then click firebase for web app you will get code just copy and paste the code and paste in firebaseConfig object above 并从您的firebase帐户单击概述链接,然后单击firebase for web app您将获得代码只需复制并粘贴代码并粘贴到上面的firebaseConfig对象中

Step 4: => Add the following to your component html 第4步:=>将以下内容添加到组件html中

<o2-upload-to-fbs [fbsBasePath] = "'images/'" [btnSelectText] = "'Browse'"></o2-upload-to-fbs>

Step:5 => Lastly in the firebase storage Rule replace the following code 步骤:5 =>最后在firebase存储规则中替换以下代码

service firebase.storage {
 match /b/your_project_name.appspot.com/o {
   match /images/{allPaths=**} {
   allow read, write;
  }
 }
}

Hope this will help you. 希望这会帮助你。

Thanks 谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM