如何在Angularjs中使用Dropzone显示已经存储在服务器上的文件
How to show files already stored on server with Dropzone in Angularjs
我使用此指令在页面中呈现Dropzone.js
:
angular.module('dropzone', []).directive('dropzone', function () {
return function (scope, element, attrs) {
var config, dropzone;
config = scope[attrs.dropzone];
// create a Dropzone for the element with the given options
dropzone = new Dropzone(element[0], config.options);
// bind the given event handlers
angular.forEach(config.eventHandlers, function (handler, event) {
dropzone.on(event, handler);
});
};
});
在我的控制器中使用以下代码:
angular.module('app', ['dropzone']);
angular.module('app').controller('SomeCtrl', function ($scope) {
$scope.dropzoneConfig = {
'options': { // passed into the Dropzone constructor
'url': 'upload.php'
},
'eventHandlers': {
'sending': function (file, xhr, formData) {
},
'success': function (file, response) {
}
}
};
});
在Dropzone中显示已存储在服务器上的文件,请使用mockFile
并为此this.emit
。现在如何获取this
并运行emit
函数?
我解决了这个问题:
'use strict';
angular.module('dropzone', []).directive('dropzone', function ($timeout) {
return {
restrict:'AE',
require: 'ngModel',
link:function (scope, element, attrs, ngModel) {
var init = function () {
var config, dropzone;
config = scope[attrs.dropzone];
// create a Dropzone for the element with the given options
dropzone = new Dropzone(element[0], config.options);
// Display existing files on server
if(ngModel.$viewValue !=='' && ngModel.$viewValue !==undefined){
var mockFile = {name: ngModel.$viewValue, size: 1234};
dropzone.emit("addedfile", mockFile);
dropzone.createThumbnailFromUrl(mockFile, "uploads/" + ngModel.$viewValue);
dropzone.emit("complete", mockFile);
}
// Form submit rest dropzone event handler
scope.$on('dropzone.removeallfile', function() {
dropzone.removeAllFiles();
});
// bind the given event handlers
angular.forEach(config.eventHandlers, function (handler, event) {
dropzone.on(event, handler);
});
};
$timeout(init, 0);
}
}
});
在控制器中:
$scope.dropzoneConfig = {
options: { // passed into the Dropzone constructor
url: '/api/uploadimage',
paramName: "file", // The name that will be used to transfer the file
maxFilesize: .5, // MB
acceptedFiles: 'image/jpeg,image/png,image/gif',
maxFiles: 1,
},
'eventHandlers': {
'removedfile': function (file,response) {
$http({
method : "POST",
url : "/api/uploadimage/"+$scope.customer.avatar_url
}).then(function mySucces(response) {
$scope.deleteMessage = response.data;
$scope.customer.avatar_url='';
});
},
'success': function (file, response) {
$scope.customer.avatar_url = response.filename;
}
}
};
并在您的 HTML 中:
<div ng-if="customer.avatar_url!==undefined" ng-model="customer.avatar_url" dropzone="dropzoneConfig" class="dropzone"></div>
相关文章:
- Javascript配置服务器URL并将其存储
- 如何通过服务器上的Node从客户端JavaScript上传到谷歌云存储
- 如何使用laravel重命名已存储在服务器中的文件
- 如何离线存储数据库,在线时共享到服务器
- 在WebSQL服务器中存储JSON数据
- 捕获视频并将其存储在服务器上
- 通过Ajax将本地存储的图像发送到服务器
- Angular:将对象存储在服务中以在视图中显示和将crud函数存储到服务器的最佳方式是什么
- 在使用java使用dropbox选择器从dropbox中提取文件后,如何将文件存储在服务器中
- 编辑存储在服务器上的文本文件
- 如何将基于Flash的游戏数据存储到服务器中
- 如何在没有Ajax的情况下将blob存储在表单中,并在单击提交时将其发送到服务器
- 如何从本地文件夹(磁盘存储)读取或获取图像文件,并使用nodejs服务器将其发送到Angularjs
- 使用html/javascript/mongodb将图像存储在服务器上
- 保护Javascript库存储在服务器上,用户即可登录
- 将数据存储在javascript中以供服务器端处理
- 用于处理QRCode生成服务器端并存储在数据库中的库
- 使用科尔多瓦在我的网络服务器上存储 GCM ID 不起作用
- 如何在浏览器中存储服务器HTTP响应头
- 卡夫卡是什么?它是数据存储服务器吗