ionic2 APP头像上传简单样例

app头像上传主要步骤如下,不说废话,直接贴代码大笑


安装命令如下:

1.

$ ionic plugin add http://github.com/don/cordova-filechooser.git
$ npm install --save @ionic-native/file-chooser

2.

$ ionic plugin add cordova-plugin-filepath
$ npm install --save @ionic-native/file-path
3.

$ ionic plugin add cordova-plugin-camera
$ npm install --save @ionic-native/camera
4.
$ ionic plugin add cordova-plugin-file-transfer
$ npm install --save @ionic-native/transfer


//读取相册文件夹

  getPhoto(){
FileChooser.open()
 .then(uri =>{
 FilePath.resolveNativePath(uri)
       .then(filePath => {                 
         this.imageURL = filePath;
         this.photoUrl = filePath;
         this.upload(this.imageURL)
       });
   }
 )
 .catch(e => console.log(e));

  }


 //拍照
  takePhoto(){
      Camera.getPicture().then((imageData) => {
           this.imageURL = imageData;
           this.photoUrl = imageData;
           this.upload(this.imageURL)
      }, (err) => {
           console.log(err);
      });
  }



  upload(imgUrl :any){
 let loader = this.loadingCtrl.create({
     content: "正在上传头像...",
   });
   loader.present();
      var ft = new Transfer();
      var options = {
      fileKey: 'file',
      fileName: this.phone+'_head.jpg',
      params:{operatiune:'uploadpoza'}
       }
      ft.upload(imgUrl,encodeURI(this.config.server +"/uploadFile/upload"),options)
      .then((data) => {
     if(data.response){
     var response=JSON.parse(data.response)
     if(response.rtn){
     loader.dismiss();
     alert("头像设置完成"); 
     }else{
     loader.dismiss();
     alert("头像设置失败,请重新登录"); 
     }
     }else{
     loader.dismiss();
     alert("头像设置失败,请重新登录");
     }
     var rtnString=JSON.stringify(data); 
     
     
       }, (err) => {
      loader.dismiss();
           alert("头像设置失败,请重新登录");
       })
   }



  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值