plupload - 限制所选文件的数量
这是代码的一部分:
inprogress = false;
function getid(id) {
return document.getElementById(id);
}
var uploader = new plupload.Uploader({
runtimes : 'gears,html5,flash,silverlight,browserplus',
browse_button : 'link-browse',
max_file_size : '100mb',
url : 'site/upload/process.php?dir=<?php echo $uplid; ?>',
flash_swf_url : 'site/upload/plupload.flash.swf',
silverlight_xap_url : 'site/upload/plupload.silverlight.xap',
});
uploader.bind('Init', function(up, params) {
//$('filelist').innerHTML = "<div>Current runtime: " + params.runtime + "</div>";
});
uploader.bind('FilesAdded', function(up, files) {
if(uploader.files.length <= 0){
var element = document.getElementById('standby');
element.parentNode.removeChild(element);
}
if(up.files.length > 4 || uploader.files.length > 4)
{
alert('Only 5 files per upload !');
return false;
}
for (var i in files) {
getid('filelist').innerHTML += '<div class="item" id="' + files[i].id + '"><div class="name">' + files[i].name + '</div><div onclick="removeme(\''+files[i].id+'\')" id="remove-'+files[i].id+'" class="remove"></div><div class="size">[ ' + plupload.formatSize(files[i].size) + ' ]</div><div class="percent"></div></div>';
}
});
uploader.bind('UploadFile', function(up, file) {
getid('submit-form').innerHTML += '<input type="hidden" name="file-' + file.id + '" value="' + file.name + '" />';
});
uploader.bind('UploadProgress', function(up, file) {
getid(file.id).getElementsByTagName('div')[3].innerHTML = '<span>' + file.percent + "%</span>";
});
uploader.bind('StateChanged', function(uploader) {
if (uploader.files.length === (uploader.total.uploaded + uploader.total.failed)) {
window.location = "./dl/<?php echo $uplid; ?>"
}
});
getid('link-upload').onclick = function() {
if(uploader.files.length < 1){
alert('Please select files first.');
return false;
}
inprogress = true;
uploader.start();
return false;
};
uploader.init();
function removeme(id){
if(inprogress) return false;
if(uploader.files.length == 1)
getid('filelist').innerHTML += '<div id="standby"></div>';
var element = document.getElementById(id);
element.parentNode.removeChild(element);
var toremove = '';
for(var i in uploader.files){
if(uploader.files[i].id === id){
toremove = i;
}
}
uploader.files.splice(toremove, 1);
}
我可以限制上传的文件, 如果我选择了 4 个文件,并且我再选择 5 个 ->它将显示错误,
但如果我首先选择 14 个文件,它们将显示在“文件列表”中。
如何限制这一点,或者在哪里放置“return false”;
感谢您的帮助:)
This is the part of code:
inprogress = false;
function getid(id) {
return document.getElementById(id);
}
var uploader = new plupload.Uploader({
runtimes : 'gears,html5,flash,silverlight,browserplus',
browse_button : 'link-browse',
max_file_size : '100mb',
url : 'site/upload/process.php?dir=<?php echo $uplid; ?>',
flash_swf_url : 'site/upload/plupload.flash.swf',
silverlight_xap_url : 'site/upload/plupload.silverlight.xap',
});
uploader.bind('Init', function(up, params) {
//$('filelist').innerHTML = "<div>Current runtime: " + params.runtime + "</div>";
});
uploader.bind('FilesAdded', function(up, files) {
if(uploader.files.length <= 0){
var element = document.getElementById('standby');
element.parentNode.removeChild(element);
}
if(up.files.length > 4 || uploader.files.length > 4)
{
alert('Only 5 files per upload !');
return false;
}
for (var i in files) {
getid('filelist').innerHTML += '<div class="item" id="' + files[i].id + '"><div class="name">' + files[i].name + '</div><div onclick="removeme(\''+files[i].id+'\')" id="remove-'+files[i].id+'" class="remove"></div><div class="size">[ ' + plupload.formatSize(files[i].size) + ' ]</div><div class="percent"></div></div>';
}
});
uploader.bind('UploadFile', function(up, file) {
getid('submit-form').innerHTML += '<input type="hidden" name="file-' + file.id + '" value="' + file.name + '" />';
});
uploader.bind('UploadProgress', function(up, file) {
getid(file.id).getElementsByTagName('div')[3].innerHTML = '<span>' + file.percent + "%</span>";
});
uploader.bind('StateChanged', function(uploader) {
if (uploader.files.length === (uploader.total.uploaded + uploader.total.failed)) {
window.location = "./dl/<?php echo $uplid; ?>"
}
});
getid('link-upload').onclick = function() {
if(uploader.files.length < 1){
alert('Please select files first.');
return false;
}
inprogress = true;
uploader.start();
return false;
};
uploader.init();
function removeme(id){
if(inprogress) return false;
if(uploader.files.length == 1)
getid('filelist').innerHTML += '<div id="standby"></div>';
var element = document.getElementById(id);
element.parentNode.removeChild(element);
var toremove = '';
for(var i in uploader.files){
if(uploader.files[i].id === id){
toremove = i;
}
}
uploader.files.splice(toremove, 1);
}
I can limit of files being uploaded,
And if I have 4 files selected, and I select 5 more -> it will show error
but if I at first select for example 14 files, they will be shown in "filelist".
How to limit that, or where to put "return false";
Thanks for any help :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
- 为Pupload v2.0
工作- Working for Pupload v2.0
将
if(up.files.length > 4 || uploader.files.length > 4)
展开为if(up.files.length > 4 || uploader.files.length > 4 || 文件长度 > 4)。
Expand
if(up.files.length > 4 || uploader.files.length > 4)
toif(up.files.length > 4 || uploader.files.length > 4 || files.length > 4)
.只需使用 max_file_count: 4 选项来限制上传的文件数量
Just use max_file_count: 4 option to limit amount files to upload
回复 Rob W 的回答,
up
和uploader
是相同的,因为它是 uploader 实例,因此是多余的;检查(uploader.files.length + files.length) > 也会很有用。 4
,以便在考虑到已经“注册”的文件时检查几个传入文件是否会超过 4(例如,有人添加 2 个文件,然后添加 3 个文件)。所以总而言之,我会推荐
in reply to Rob W's answer,
up
anduploader
is the same since it's the uploader instance, hence redundant; and it would be useful to also check(uploader.files.length + files.length) > 4
in order to check if a couple of incoming files would exceed the 4 when taking the already "registered" files into account (e.g. somebody adds 2 files and then 3 files subsequently).So in conclusion, I would recommend