How to Implement angularjs Image Upload and obtain the image path how to implement angularjs Image Upload and obtain the image path
Reply content:
How to upload angularjs images and obtain image paths
Generally, I write a form submission by myself. The main principle is to use a hidden input and then write the command to the input. The code can be referred to below:
Function checkImgType (file) {var type = file. name. split ("."); if (type [type. length-1]! = 'Png '& type [type. length-1]! = 'Jpg '& type [type. length-1]! = 'Jpeg ') {return false;} return true ;}; $ scope. upload = function (name, imgUrl) {var fileObj = document. getElementById (name ). files [0]; if (fileObj) {$ scope. imgloading [imgUrl] = true;} if (! CheckImgType (fileObj) {alert ("the image format is incorrect. Only png, jpg, and jpeg formats are supported."); return;} var form = new FormData (); form. append ("file", fileObj); var xhr = new XMLHttpRequest (); xhr. open ("POST", httpBase + "upload", true); function callback () {if (xhr. readyState = 4) {$ timeout (function () {var data = angular. fromJson (xhr. responseText); $ scope. $ apply (function () {$ scope. goods [name] = data. statusMessage;}) ;}, 3000) ;}; xhr. onreadystatechange = callback; xhr. send (form );};
Html
Add commands
.directive("ltFile", function() { return { restrict: 'AE', link: function(scope, elm, attr, ngModelCtrl) { elm.bind('change', function(newValue,oldValue) { scope.$apply(function() { eval("scope." + attr.fileFn); }) }); } };})
https://github.com/danialfarid/ng-file-upload