MUI take pictures or choose photos to upload as Avatar

Source: Internet
Author: User

1. The first method is implemented using H5

Html:

<label>

<input style= "opacity:0;" type= "File" accept= "image/*" id= "Head_img_change"/>

</label>

Js:

$ (function () {

$ ("#head_img_change"). Change (function () {
var $file = $ (this);
var fileobj = $file [0];
var windowurl = window. URL | | Window.webkiturl;
Var Dataurl;
var $img = $ ("#headimg");
if (fileobj && fileobj.files && fileobj.files[0]) {
Dataurl = Windowurl.createobjecturl (Fileobj.files[0]);
$img. attr (' src ', dataurl);        
}else{
Dataurl = $file. val ();
var imgobj = document.getElementById ("headimg");
//1, when setting the Filter property, the element must already exist in the DOM tree, the dynamically created node, also need to be added to the DOM before setting the property, first set the property to join, invalid;
//2, the SRC attribute needs to be added as follows, two of the above        method added, invalid;
ImgObj.style.filter = "Progid:DXImageTransform.Microsoft.AlphaImageLoader (Sizingmethod=scale)";
ImgObj.filters.item ("DXImageTransform.Microsoft.AlphaImageLoader"). src = dataurl;
}
});

})

  description : 1) Add a label externally to trigger input in the range when the user clicks

2) H5 input file upload image, call api:<input type= "file" accept= "Image/*;capture=camera" > Call camera directly
<input type= "File" accept= "image/*"/> Call camera, picture or album

Note: Some Android phones can only call local albums, the camera can not call or only call the camera, cannot call album.

3) This method is a method on the net, does the compatibility, IE6 above all can run normally.

2. Second method: MUI

1) Content Link: http://www.bcty365.com/content-146-3648-1.html

2) You need to change the ID name of the trigger object in the avatar trigger to change it to your own.

Also need to modify the upload image in a little bit of code can be used:

    var mainimage= document.getelementbyid ("headimg");

MAINIMAGE.SRC = Imgpath;

    explanation : This part changed to the above two sentences, because I originally put the picture of the place has wide height, mainimage give it a picture of the object on the line.

MUI take pictures or choose photos to upload as Avatar

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.