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("头像设置失败,请重新登录");
})
}