【发布时间】:2017-03-08 21:09:45
【问题描述】:
Dropzone.options.dropzone = {
uploadMultiple: false,
maxFiles: {{ imagecount }},
maxFilesize: 2, // MB
dictFileTooBig: "That image is too large. We only allow 2MB or smaller.",
acceptedFiles: ".jpeg, .jpg",
init: function () {
this.on("success", function(file, response) {
$('.uploaded-photos').append('<div><img src="' + response.link + '"height="150" class="photo" id="' + response.id + '"/><span class="delete-photo">Delete</span></div>');
$('.uploaded-photos').justifiedGallery('norewind')
this.removeFile(file);
var grabNewCount = parseInt($('.recordcount').text(), 10)
$('.recordcount span').text(grabNewCount += 1);
});
}
};
我尝试在每次上传/删除新图像时动态更新 MaxFiles 属性。
如您所见,当页面加载时,我正在从我的服务器传递{{ imagecount }}。这只是检查已经为用户上传了多少图像,从而计算了他们还有多少要上传。在第一页加载时,这有效。
当我删除使用 AJAX 执行的图像时,问题似乎出现了。我似乎无法更新该 maxFiles 值。如果我最初允许 10 张图片并且用户当前上传了 5 张并删除了一张,那么新的 maxFiles 值现在应该是 6,但我所做的任何事情似乎都不会影响动态更新该值。这是我的删除代码:
$('.delete-photo').click(function() {
$(this).text('') // remove "delete" text from button
$(this).html('<i class="fa fa-spinner fa-spin"></i>') // add spinning icon to indicate "working"
var csrf = $('[name=csrfmiddlewaretoken]').val();
$.ajax({
context: this,
type: "POST",
url: '/delete-photo/',
data: {'csrfmiddlewaretoken': csrf, 'id': $(this).parent().attr('id') },
dataType: "json",
timeout: 10000,
cache: false,
success: function(data, textStatus, XMLHttpRequest){
var image = $(this).parent();
$(image).fadeOut(800, function() {
$(this).remove();
$('.uploaded-photos').justifiedGallery()
});
Dropzone.options.dropzone.maxFiles = Dropzone.options.dropzone.maxFiles + 1;
var grabNewCount = parseInt($('.recordcount').text(), 10)
$('.recordcount span').text(grabNewCount -= 1);
}
});
});
所以你可以看到我有Dropzone.options.dropzone.maxFiles = Dropzone.options.dropzone.maxFiles + 1; 试图获取当前的maxFiles 值并更新值+1,但这并没有做任何事情。
有什么想法吗?
【问题讨论】:
标签: jquery ajax dropzone.js