plupload 上传单个图像文件
plupload upload a single image file
我有以下 fiddle 我一直在尝试修改:
我正在尝试获得此功能:
1> 用户点击选择图像按钮并上传文件(正在处理)
2> 用户再次点击选择图像按钮,现有文件被新文件替换。
3> 用户点击删除按钮,文件被删除。
我正在努力研究如何做到这一点。代码如下所示:
$(document).ready(function() {
var uploader = new plupload.Uploader({
runtimes : 'html5,flash,silverlight',
browse_button : 'pick-files',
max_file_size : '1mb',
multi_selection: false,
max_file_count: 1,
unique_names : true,
autostart: true,
url: "/echo/json",
flash_swf_url : 'http://rawgithub.com/moxiecode/moxie/master/bin/flash/Moxie.cdn.swf',
silverlight_xap_url : 'http://rawgithub.com/moxiecode/moxie/master/bin/silverlight/Moxie.cdn.xap',
filters : [
{title : "Image files", extensions : "jpg,png"}
],
init: {
PostInit: function() {
document.getElementById('pick-files').style.visibility = 'visible';
},
FilesAdded: function(up, files) {
plupload.each(files, function(file) {
if(uploader.files.length > 1){
//uploader.removeFile(uploader.getFile(this.id));
console.log(file.id);
// return;
}
var img = new o.Image();
img.onload = function() {
// create a thumb placeholder
var li = document.createElement('li');
li.id = this.uid;
document.getElementById('gallery').appendChild(li);
// embed the actual thumbnail
this.embed(li.id, {
width: 100,
height: 60,
crop: true
});
};
img.load(file.getSource());
});
},
Error: function(up, err) {
document.getElementById('console').innerHTML += "\nError #" + err.code + ": " + err.message;
}
}
});
uploader.init();
});
我试过这个。你走在正确的道路上,但看起来你正在删除你正在添加的文件,而队列中已经有一个文件。当 uploader.files.length > 1 时,您也不想 "return"。这会将您带出 "each" 并且其余代码不是 运行.
第三部分是删除文件。为此,我绑定了按钮单击事件以删除文件。
这里是 Fiddle。
这里是一些相关的代码片段。
FilesAdded: function(up, files) {
plupload.each(files, function(file) {
if(uploader.files.length > 1){
removeImage(); //Call the removeImage() function and then continue
}
然后删除图像,这就是我所做的。
uploader.init(); //<-- After initialising
document.getElementById('remove').onclick = function () {
removeImage();
};
function removeImage(){
if(uploader.files.length > 0){
uploader.removeFile(uploader.files[0].id);
document.getElementById('gallery').innerHTML = ''; //This will remove it from the page
}
}
那只是针对你只有一张图片的情况快速删除它。可能有更好的方法来重置图库,但我发现仅调用 removeFile 并不能将其从页面中删除,您需要重置 innerHTML 或将其从列表中拼接等。
我有以下 fiddle 我一直在尝试修改:
我正在尝试获得此功能:
1> 用户点击选择图像按钮并上传文件(正在处理)
2> 用户再次点击选择图像按钮,现有文件被新文件替换。
3> 用户点击删除按钮,文件被删除。
我正在努力研究如何做到这一点。代码如下所示:
$(document).ready(function() {
var uploader = new plupload.Uploader({
runtimes : 'html5,flash,silverlight',
browse_button : 'pick-files',
max_file_size : '1mb',
multi_selection: false,
max_file_count: 1,
unique_names : true,
autostart: true,
url: "/echo/json",
flash_swf_url : 'http://rawgithub.com/moxiecode/moxie/master/bin/flash/Moxie.cdn.swf',
silverlight_xap_url : 'http://rawgithub.com/moxiecode/moxie/master/bin/silverlight/Moxie.cdn.xap',
filters : [
{title : "Image files", extensions : "jpg,png"}
],
init: {
PostInit: function() {
document.getElementById('pick-files').style.visibility = 'visible';
},
FilesAdded: function(up, files) {
plupload.each(files, function(file) {
if(uploader.files.length > 1){
//uploader.removeFile(uploader.getFile(this.id));
console.log(file.id);
// return;
}
var img = new o.Image();
img.onload = function() {
// create a thumb placeholder
var li = document.createElement('li');
li.id = this.uid;
document.getElementById('gallery').appendChild(li);
// embed the actual thumbnail
this.embed(li.id, {
width: 100,
height: 60,
crop: true
});
};
img.load(file.getSource());
});
},
Error: function(up, err) {
document.getElementById('console').innerHTML += "\nError #" + err.code + ": " + err.message;
}
}
});
uploader.init();
});
我试过这个。你走在正确的道路上,但看起来你正在删除你正在添加的文件,而队列中已经有一个文件。当 uploader.files.length > 1 时,您也不想 "return"。这会将您带出 "each" 并且其余代码不是 运行.
第三部分是删除文件。为此,我绑定了按钮单击事件以删除文件。
这里是 Fiddle。
这里是一些相关的代码片段。
FilesAdded: function(up, files) {
plupload.each(files, function(file) {
if(uploader.files.length > 1){
removeImage(); //Call the removeImage() function and then continue
}
然后删除图像,这就是我所做的。
uploader.init(); //<-- After initialising
document.getElementById('remove').onclick = function () {
removeImage();
};
function removeImage(){
if(uploader.files.length > 0){
uploader.removeFile(uploader.files[0].id);
document.getElementById('gallery').innerHTML = ''; //This will remove it from the page
}
}
那只是针对你只有一张图片的情况快速删除它。可能有更好的方法来重置图库,但我发现仅调用 removeFile 并不能将其从页面中删除,您需要重置 innerHTML 或将其从列表中拼接等。