IONIC3 Study Notes (16) upload avatar to the image bed

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.