Hi I need when select a file show a preview of the file when I select a image or pdf document:
angular.module('HelloWorldApp', [])
.controller('HelloWorldController', function($scope, $http, $window, $sce) {
$scope.extensao = '';
$scope.uploadImagem = function (element) {
var photofile = element.files[0];
var reader = new FileReader();
$scope.files = []
$scope.files.push(element.files[0])
$scope.extensao = element.files[0].name;
$scope.type = '';
reader.onload = function (e) {
$scope.imagem = e.target.result;
$scope.$apply();
};
reader.readAsDataURL(photofile);
};
$scope.showImage = function () {
if ($scope.extensao.toLowerCase().includes('.jpg'))
$scope.type = 'image/jpg';
if ($scope.extensao.toLowerCase().includes('.png'))
$scope.type = 'image/png';
if ($scope.extensao.toLowerCase().includes('.pdf'))
$scope.type = 'application/pdf';
if ($scope.extensao.toLowerCase().includes('.doc'))
$scope.type = 'application/msword';
var length = $scope.imagem.length;
var arrayBuffer = new ArrayBuffer(length);
var uintArray = new Uint8Array(arrayBuffer);
for (var i = 0; i < length; i++) {
uintArray[i] = $scope.imagem.charCodeAt(i);
}
var file = new Blob([uintArray], { type: $scope.type });
var fileURL = URL.createObjectURL(file);
$scope.pdfContent = $sce.trustAsResourceUrl(fileURL);
}
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.9/angular.min.js"></script>
<div ng-app="HelloWorldApp">
<div ng-controller="HelloWorldController">
<label for="fileToUpload">
<input type="file" style="display:none;" ng-model-instant id="fileToUpload" multiple onchange="angular.element(this).scope().uploadImagem(this)" />
Select File
</label>
<div ng-show="files.length" class="form-group col-xs-10">
<div ng-repeat="file in files.slice(0)">
<span class='label label-info'>{{file.webkitRelativePath || file.name}}
</span>
<input type="button" value="Open" ng-click="showImage()"/>
</div>
</div>
<div class="corpoConfirma" onloadstart="">
<object data="{{pdfContent}}" type="{{type}}" style="width:100%; height:100%" />
</div>
</div>
</div>
How to read pdf stream in angularjs
This example do not show the preview is possible open new window to show, can help me please!