最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - AngularJS image upload preview without directive - Stack Overflow

programmeradmin0浏览0评论

I'm uploading files via service:

var addFile = function(files) {
  var deferred = $q.defer();
  var fd = new FormData();
  fd.append("file", files[0]);
  $http.post("/files", fd, {
      ***
    })
    .success(function(data, status, headers, config) {
      ***
    })
    .error(function(err, status) {
      ***
    });
    ***
};

and in controller I have something like:

uplService.addFile($scope.files).then(function(url) {
  $scope.news.Photo = url;
});

and in HTML view:

<input type="file" name="file" onchange="angular.element(this).scope().photoChanged(this.files)" />

before that I uploaded file on-the-go, when I select file it goes directly to server, but now I need to display it in my form when I select it, but upload later, all I see in web is using directives, but how could I organize it without using directives?

I'm uploading files via service:

var addFile = function(files) {
  var deferred = $q.defer();
  var fd = new FormData();
  fd.append("file", files[0]);
  $http.post("/files", fd, {
      ***
    })
    .success(function(data, status, headers, config) {
      ***
    })
    .error(function(err, status) {
      ***
    });
    ***
};

and in controller I have something like:

uplService.addFile($scope.files).then(function(url) {
  $scope.news.Photo = url;
});

and in HTML view:

<input type="file" name="file" onchange="angular.element(this).scope().photoChanged(this.files)" />

before that I uploaded file on-the-go, when I select file it goes directly to server, but now I need to display it in my form when I select it, but upload later, all I see in web is using directives, but how could I organize it without using directives?

Share Improve this question edited Jan 5, 2015 at 10:10 arman1991 1,1661 gold badge18 silver badges28 bronze badges asked Jan 5, 2015 at 8:29 byCoderbyCoder 9,18427 gold badges119 silver badges259 bronze badges 3
  • take a look at developer.mozilla.org/en-US/docs/Web/API/FileReader for show the file before upload and all in all I prefer using a dedicate module google.it/… – Whisher Commented Jan 5, 2015 at 8:52
  • @Whisher i need with example – byCoder Commented Jan 5, 2015 at 8:55
  • well if you need with example ... start searching based on what you just learned from link – charlietfl Commented Jan 5, 2015 at 8:57
Add a comment  | 

2 Answers 2

Reset to default 10

Can you try this in your controller to pass your file object here:

$scope.fileReaderSupported = window.FileReader != null;
$scope.photoChanged = function(files){
    if (files != null) {
        var file = files[0];
    if ($scope.fileReaderSupported && file.type.indexOf('image') > -1) {
        $timeout(function() {
            var fileReader = new FileReader();
            fileReader.readAsDataURL(file);
            fileReader.onload = function(e) {
                $timeout(function(){
                    $scope.thumbnail.dataUrl = e.target.result;
                });
            }
        });
    }
}
};

and on the view

<img ng-show="thumbnail.dataUrl != null" ng-src="{{ thumbnail.dataUrl }}" class="thumb">

demo here

Hope this help

I read this article, which helped me to solve the problem about uploading the image.

If you want to show your selected file, try this:

<img data-ng-src="data:image/png;base64,{{news.Photo}}" id="photo-id"/>

Explanation:

Your property for image in Model/ViewModel/Class must be an array of bytes, like

public byte[] Photo { get; set; }

The data:image/jpeg;base64 defines the byte array from news.Photo so it can be rendered correctly on the clients browser.

The $scope.news.Photo in your case is just an scoped variable which contains the drawed image with byte created by the byte equivalent in the $scope.uploadFile function from article.

I hope it will be also helpful for you.

发布评论

评论列表(0)

  1. 暂无评论