Angularjs Image Upload problems?

Source: Internet
Author: User
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

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.