Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

This code is working fine To display Image in Img Tag

$scope.isImage = function(ext) {
  if(ext) {
    return ext == "jpg" || ext == "jpeg"|| ext == "gif" || ext=="png"
  }
}

Here is working demo http://plnkr.co/edit/rAZVSFRURCkgxQrZAtvO?p=preview

I would like to display Image in

<div style="background-image:url(How can i get browsed image file)"></div>
share|improve this question

You need to create a path string in DOM to represent your browsed file and then add it as background image url. I got it working by modifying your code like below.

<body>
    <form action="" id="contactproForm" method="post" ng-app="myApp" ng-controller="myCtrl" enctype="multipart/form-data">

        <label for="file">Attachment</label>
        <div class="input-box">
          <input type="file" id="file" class="input-text" ngf-change="onChange(picFile)" ngf-select ng-model="picFile" name="attachement" accept="image/png, image/jpeg, image/jpg, application/msword, application/vnd.ms-excel, application/pdf " />
        </div>
        <div id="image"class="image" style="width:200px;height:200px;background-image:url('');"></div>       <!-- div with id="image"-->

    </form>


    <script>
    var app = angular.module('myApp', ['ngFileUpload']);

    app.controller('myCtrl', ['$scope', '$http', '$timeout', '$compile', 'Upload',
        function($scope, $http, $timeout, $compile, Upload) {
            $scope.onChange = function(files) {
                if (files[0] == undefined) return;
                if($scope.isImage(files[0].name.split(".").pop())){  // checking if image
                    var path = URL.createObjectURL(files[0]);       //  creating a path for file
                    document.getElementById('image').style.backgroundImage = "url(" + path + ")";   // setting it as background image of div
                }else{
                    alert("not Image");
                }
            }

            $scope.isImage = function(ext) {
                if (ext) {
                    return ext == "jpg" || ext == "jpeg" || ext == "gif" || ext == "png"
                }
            }
        }
    ]);
    </script>

</body>

I've added comments for changes made.

share|improve this answer
    
This could be improved by encapsulating the code into a custom directive – Mike Goodwin 21 hours ago
    
Also, since the OP is using ng-file-upload, can you use the ngf-background directive? github.com/danialfarid/ng-file-upload – Mike Goodwin 20 hours ago

use ng-style instead of style like this and replace image url.

ng-style="{'background-image':'url(https://www.google.com/images/srpr/logo4w.png)'}">

share|improve this answer
    
This won't work for images stored on the users local machine. You need to open the file, read it's data and create a data URL like in the answer of @Nijeesh. – Mike Goodwin 21 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.