0
我见过很多这样的例子,但似乎无法让我的工作。使用FileReader和AngularJS显示图像
我有一个包含图像信息的对象数组。例如:
$scope.images = [
{
"id": 1,
"type": "cloud",
"caller_id": "[email protected]",
"image_path": "file:///C:\\Users\\Public\\Pictures\\Sample Pictures\\Chrysanthemum.jpg",
"image_results": 23,
"image_id": 3243242
},
{
"id": 2,
"type": "cloud",
"caller_id": "[email protected]",
"image_path": "file:///C:\\Users\\Public\\Pictures\\Sample Pictures\\Desert.jpg",
"image_results": 98,
"image_id": 3125312
},
{
"id": 3,
"type": "cloud",
"caller_id": "[email protected]",
"image_path": "file:///C:\\Users\\Public\\Pictures\\Sample Pictures\\DSC_9864.jpg",
"image_results": 1,
"image_id": 927192
}
];
我需要能够在图像库中显示这些图像。我一直在模仿我的画廊this gallery,供参考。
我想这样做在我的控制下,以显示第一图像:
//Image information
$scope.currentImage = _.first($scope.images);
//Display image
var reader = new FileReader();
reader.onload = function(event) {
$scope.$apply(function() {
$scope.image_source = reader.result;
});
};
reader.readAsDataURL($scope.currentImage.image_path);
$scope.setCurrentImage = function(image){
$scope.currentImage = image;
}
在我的HTML我有:
<div class="container">
<div class="row">
<div class="col-xs-12">
<div ng-controller="jobsSampleImageController">
<h2>AngularJS Powered Photo Album <small>by Josh Adamous</small> </h2>
<div class="row">
<div class="col-md-9">
<div class="albumImage">
<img id="current_image" ng-src="{{image_source}}" alt="{{currentImage.caller_id}}"/>
</div>
<h3>{{currentImage.caller_id}}</h3>
</div><!-- End of Column -->
<div class="col-md-3">
<div class="wrapper">
<ul class="list">
<li ng-repeat="image in images" ng-click="setCurrentImage(image)">
<img ng-src="{{image_source}}" alt="{{image.caller_id}}"/>
</li>
</ul><!-- End of List -->
</div><!-- End of Wrapper -->
</div><!-- End of Column -->
</div><!-- End of Row -->
</div><!-- End of Album Controller -->
</div><!-- End of Column -->
</div><!-- End of Row -->
</div><!-- End of Container -->
这将导致一个错误:
TypeError: Failed to execute 'readAsDataURL' on 'FileReader': parameter 1 is not of type 'Blob'.
从我看到的例子来看,我不完全确定他们什么时候得到图像blob?
我不认为你可以在这个实例中使用'file://类型的路径。 – Claies
@Claies删除您误解的file:///' –
后,我会得到相同的结果。我的意思是,浏览器无法访问计算机本地硬盘('file://路径)上的图像,只能访问网络上的图像('http://路径)。换句话说,在浏览器中无法访问'C:\\ Users \\ Public \\ Pictures \\ Sample Pictures \\ Desert.jpg'。 – Claies