ZH cheese: angularjs/javascript upload picture "PC Side"

Source: Internet
Author: User



"Introduction to Features"



Similar to the modification of personal information, click on the avatar, you can complete the selection of photos, upload photos and other steps to achieve the purpose of replacing the avatar.



"Run Process"



(1) Click the Avatar



(2) Select Avatar



(3) Click "Done" to upload your avatar



1.HTML Image section of the code (personal information will also include the name [id= "name"], Introduction [id= "Intro"] element, etc.)


 
 
<div>
    <img id="avatar"ng-src="{{avatar}}" ng-click="choosePicMenu()">
</div>
<input ng-hide="true" type="file" id="photoBtn" accept="image/*"onchange="picChange(event,‘avatar‘)"/>


2.js Core Code



(1) Select picture


 
//Click on the picture, send the click() event to the element id = "photobtn", open the file selection window, and select the picture
$scope.choosePicMenu = function() {
$(‘#photoBtn‘).click();
}
//After selecting the image, the input element changes. Activate the onchange attribute and execute the picchange (event, 'Avatar') function
//Where event refers to the onchange event, and event.target refers to the element where the event occurs. For more information about event, please click: portal
function picChange(event,imgId){
var files = event.target.files;
if(files &amp;&amp; files.length >= 1){
var file = files[0];
//Load image is a function of the JavaScript plug-in JavaScript load image
loadImage.parseMetaData(
File,
function (data) {
if (!data.imageHead) {
LoadImage (
File,
function (img) {
var URL = window.URL || window.webkitURL;
var imgURL = img.toDataURL("image/png");
$(‘#‘+imgId).attr(‘src‘, imgURL);
$(‘#‘ + imgId).attr(‘data-change‘, 1);
}
{maxWidth: 600,
canvas: true} // Options
);
Return;
}
// Combine data.imageHead with the image body of a resized file
// to create scaled images with the original image meta data, e.g.:
// get orietation info.
if (data.exif &amp;&amp; data.exif[0x0112]) {
var orientation = data.exif[0x0112];
LoadImage (
File,
function (img) {
var URL = window.URL || window.webkitURL;
var imgURL = img.toDataURL("image/png");
$(‘#‘+imgId).attr(‘src‘, imgURL);
$(‘#‘ + imgId).attr(‘data-change‘, 1);
}
{maxWidth: 600,
canvas: true,
orientation:orientation} // Options
);
} else {
LoadImage (
File,
function (img) {
var URL = window.URL || window.webkitURL;
var imgURL = img.toDataURL("image/png");
$(‘#‘+imgId).attr(‘src‘, imgURL);
$(‘#‘ + imgId).attr(‘data-change‘, 1);
}
{maxWidth: 600,
canvas: true} // Options
);
}
}
);
}
}


(2) Uploading images


//Create a new formdata object
var fd = new FormData();
//If the image is modified, the "data change" attribute of the element id = "Avatar" will be true
if($(‘#avatar‘).attr(‘data-change‘)){
var files = document.getElementById(‘photoBtn‘).files;
if(files &amp;&amp; files.length >= 1){
FD. Append ('ffile ', files [0]); / / add the image to the formdata object
}
}
//Get name, introduction and other elements
var name = $(‘#name‘).val();
var intro = $(‘#intro‘).val();
If (name) {
fd.append(‘name‘,name);
}
If (Intro) {
fd.append(‘introduction‘,intro);
}
//Sending a request using the XMLHttpRequest object
var xhr = new XMLHttpRequest();
xhr.addEventListener("load", $scope.updateComplete, false);
xhr.open("POST", "some url");
xhr.send(fd);
$scope.updateComplete = function(evt){
var resp = evt.target.responseText;
var respJson = $.parseJSON(resp);
if(respJson.status == 0){
console.log("success!");
}else{
console.log("fail");
}
} 





ZH cheese: angularjs/javascript upload picture "PC Side"


Related Article

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.