This article is original article, reprint please indicate the source
The Open source Demo login module used by the individual is the identity authentication service of the Wilddog Wild Dog Communication Cloud, and has to say that all aspects are similar to the Firebase acquired by Google, very simple and easy to use. One of the User
photoURL
fields was used to store the user's avatar URL, so I was thinking about finding a free third-party bed (sm.ms) to store the avatar.
Use of the Cordova plug-in is the camera and File Transfer, respectively, to take photos, album selection and upload pictures, Cordova plug-in installation, import, use I do not repeat, the document has, so directly on the key code.
Getpictureandupload (sourcetype:number) {Const Cameraoptions:cameraoptions = {quality:80, destinationty Pe:this.camera.DestinationType.FILE_URI, Sourcetype:sourcetype, Allowedit:true, EncodingType:this.camer A.encodingtype.jpeg, targetwidth:200, targetheight:200, MediaType:this.camera.MediaType.PICTURE, CO Rrectorientation:true, Savetophotoalbum:true, CameraDirection:this.camera.Direction.BACK}; This.camera.getPicture (cameraoptions). Then (image = {this.onuploadpicture (image); }, Error = {console.log (error); }); } onuploadpicture (fileuri:string) {Const FILETRANSFEROBJECT:FILETRANSFEROBJECT = this.fileTransfer.create (); Const Fileuploadoptions:fileuploadoptions = {filekey: ' file ', FileName: ' Avatar.jpg ', HttpMethod: ' POST ', MimeType: ' Image/jpeg ', params: {}, Chunkedmode:true, headers: {' content-type ': ' Multipart/form-data ‘} }; Leturl:string = ' https://sm.ms/api/upload?smfile= ' + Fileuri; Filetransferobject.upload (Fileuri, URL, fileuploadoptions). Then (data = {Console.log (data["response"]); Wilddog.auth (). onauthstatechanged (user + = {user.updateprofile ({' Photourl ': Json.parse (data["Response"]) ["Data" ["url"]}). then (() = {This.getuserdata (); }, Error = {this.presenttoast (error.name + ': ' + error.message); }); }); }, Error = {console.log (error); }); } presentchangeavataractionsheet () {Let Changeavataractionsheet = This.actionSheetCtrl.create ({title: ' Change avatar ' Buttons: [{text: ' album ', Handler: () = {This.getpictureandupload (this.camera.PictureSourceType. Photolibrary); }}, {text: ' Take photo ', handler: () = {This.getpictureandupload (this.camera.picturesourcetype.c Amera); }}, {text: ' Cancel ', role: ' Cancel '}}); ChangeavataractionsHeet.present (). then (value = = {return value; }); } }
If there is any improper, please correct, thank you ~
IONIC3 Study Notes (16) upload avatar to the image bed