bra*_*r19 2 javascript image angularjs
我通过服务上传文件:
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) {
***
});
***
};
Run Code Online (Sandbox Code Playgroud)
在控制器中我有类似的东西:
uplService.addFile($scope.files).then(function(url) {
$scope.news.Photo = url;
});
Run Code Online (Sandbox Code Playgroud)
并在HTML视图中:
<input type="file" name="file" onchange="angular.element(this).scope().photoChanged(this.files)" />
Run Code Online (Sandbox Code Playgroud)
在此之前我上传了文件,当我选择文件时它直接进入服务器,但现在我需要在我选择它时以我的形式显示它,但是稍后上传,我在网上看到的只是使用指令,但是如何在不使用指令的情况下组织它?
mur*_*nax 10
您可以在控制器中尝试将此文件对象传递到此处:
$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;
});
}
});
}
}
};
Run Code Online (Sandbox Code Playgroud)
并在视图上
<img ng-show="thumbnail.dataUrl != null" ng-src="{{ thumbnail.dataUrl }}" class="thumb">
Run Code Online (Sandbox Code Playgroud)
希望这有帮助
我读了这篇文章,帮助我解决了上传图片的问题.
如果要显示所选文件,请尝试以下操作:
<img data-ng-src="data:image/png;base64,{{news.Photo}}" id="photo-id"/>
Run Code Online (Sandbox Code Playgroud)
说明:
Model/ViewModel/Class中的图像属性必须是一个字节数组,如
public byte[] Photo { get; set; }
Run Code Online (Sandbox Code Playgroud)
数据:image/jpeg; base64定义了字节数组,news.Photo因此可以在客户端浏览器上正确呈现.
在$scope.news.Photo你的情况只是一个范围的变量包含由字节相当于从文章的$ scope.uploadFile函数创建的字节drawed图像.
我希望它对你也有帮助.