使用 HTML5 文件输入时单击按钮从多个文件上传器中删除文件
Removing file from multiple files uploader on button click when using HTML5 file input
如何在此处添加删除按钮,就像这样在文件队列中一个一个地删除
我之所以不使用带 OOB 插件的免费文件上传插件,是因为我的客户要求uirements 是出于安全目的,他们需要简单的上传 ui,没有任何复杂的插件。
$(function() {
var dropZoneId = "drop-zone";
var buttonId = "clickHere";
var mouseOverClass = "mouse-over";
var dropZone = $("#" + dropZoneId);
var ooleft = dropZone.offset().left;
var ooright = dropZone.outerWidth() + ooleft;
var ootop = dropZone.offset().top;
var oobottom = dropZone.outerHeight() + ootop;
var inputFile = dropZone.find("input");
document.getElementById(dropZoneId).addEventListener("dragover", function(e) {
e.preventDefault();
e.stopPropagation();
dropZone.addClass(mouseOverClass);
var x = e.pageX;
var y = e.pageY;
if (!(x < ooleft || x > ooright || y < ootop || y > oobottom)) {
inputFile.offset({
top: y - 15,
left: x - 100
});
} else {
inputFile.offset({
top: -400,
left: -400
});
}
}, true);
if (buttonId != "") {
var clickZone = $("#" + buttonId);
var oleft = clickZone.offset().left;
var oright = clickZone.outerWidth() + oleft;
var otop = clickZone.offset().top;
var obottom = clickZone.outerHeight() + otop;
$("#" + buttonId).mousemove(function(e) {
var x = e.pageX;
var y = e.pageY;
if (!(x < oleft || x > oright || y < otop || y > obottom)) {
inputFile.offset({
top: y - 15,
left: x - 160
});
} else {
inputFile.offset({
top: -400,
left: -400
});
}
});
}
document.getElementById(dropZoneId).addEventListener("drop", function(e) {
$("#" + dropZoneId).removeClass(mouseOverClass);
}, true);
inputFile.on('change', function(e) {
$('#filename').html("");
var fileNum = this.files.length,
initial = 0,
counter = 0;
for (initial; initial < fileNum; initial++) {
counter = counter + 1;
$('#filename').append('<span class="fa-stack fa-lg"><i class="fa fa-file fa-stack-1x "></i><strong class="fa-stack-1x" style="color:#FFF; font-size:12px; margin-top:2px;">' + counter + '</strong></span> ' + this.files[initial].name + ' <span class="fa fa-times-circle fa-lg closeBtn" title="remove"></span><br>');
}
});
})
#drop-zone {
width: 100%;
min-height: 150px;
border: 3px dashed rgba(0, 0, 0, .3);
border-radius: 5px;
font-family: Arial;
text-align: center;
position: relative;
font-size: 20px;
color: #7E7E7E;
}
#drop-zone input {
position: absolute;
cursor: pointer;
left: 0px;
top: 0px;
opacity: 0;
}
/*Important*/
#drop-zone.mouse-over {
border: 3px dashed rgba(0, 0, 0, .3);
color: #7E7E7E;
}
/*If you dont want the button*/
#clickHere {
display: inline-block;
cursor: pointer;
color: white;
font-size: 17px;
width: 150px;
border-radius: 4px;
background-color: #4679BD;
padding: 10px;
}
#clickHere:hover {
background-color: #376199;
}
#filename {
margin-top: 10px;
margin-bottom: 10px;
font-size: 14px;
line-height: 1.5em;
}
.file-preview {
background: #ccc;
border: 5px solid #fff;
box-shadow: 0 0 4px rgba(0, 0, 0, 0.5);
display: inline-block;
width: 60px;
height: 60px;
text-align: center;
font-size: 14px;
margin-top: 5px;
}
.closeBtn:hover {
color: red;
}
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<div id="drop-zone">
<p>Drop files here...</p>
<div id="clickHere">or click here.. <i class="fa fa-upload"></i>
<input type="file" name="file" id="file" multiple />
</div>
<div id='filename'></div>
</div>
注意:我不拥有代码,我已经将它作为我的资源从其他人那里重新使用并为我的客户修改了它
**UPDATE
Here my fiddle link
我以前为我的 Dropzone 做过这个。随意调整。这是来自我的 Laravel 应用程序。你应该关注 avatar_refresh_upload
。剪掉不必要的东西就大功告成了。
function avatar_refresh_upload() {
var input = $('input#avatar[type=file]');
input.replaceWith(input.val('').clone(true));
$('#selected_file').html('{{ Lang::get('app.profile_avatar_select') }}');
$('#avatar_refresh_upload').removeAttr('style');
}
$(document).ready(function ($) {
$('input:file#avatar').change(function () {
var file_name = $(this).val();
if (file_name.length > 10) {
file_name = file_name.substring(0, 10) + '...';
}
$('#selected_file').html('File "' + file_name + '" chosen');
$('#avatar_refresh_upload').css('display', 'inline-block');
});
$('#avatar_refresh_upload').on('click', function () {
avatar_refresh_upload();
});
@if ($user->avatar != '')
$('#remove_avatar').change(function () {
if ($(this).is(':checked')) {
avatar_refresh_upload();
$('#avatar').prop('disabled', true);
$('#avatar_preview').css('opacity', '0.5');
$('#avatar_upload_form_area').css('opacity', '0.5');
$('#remove_avatar_info').show();
} else {
$('#avatar').prop('disabled', false);
$('#avatar_preview').removeAttr('style');
$('#avatar_upload_form_area').removeAttr('style');
$('#remove_avatar_info').removeAttr('style');
}
});
@endif
});
长话短说 - 如果您想在选择要上传的文件后但在提交之前重置输入文件,则必须 运行:
input.replaceWith(input.val('').clone(true));
$(function () {
var dropZoneId = "drop-zone";
var buttonId = "clickHere";
var mouseOverClass = "mouse-over";
var dropZone = $("#" + dropZoneId);
var ooleft = dropZone.offset().left;
var ooright = dropZone.outerWidth() + ooleft;
var ootop = dropZone.offset().top;
var oobottom = dropZone.outerHeight() + ootop;
var inputFile = dropZone.find("input");
var filesArr = [];
function showFiles() {
$('#filename').html("");
var fileNum = filesArr.length;
for (var i = 0; i < fileNum; i++) {
$('#filename').append('<div><span class="fa-stack fa-lg"><i class="fa fa-file fa-stack-1x "></i><strong class="fa-stack-1x" style="color:#FFF; font-size:12px; margin-top:2px;">'+ i + '</strong></span> ' + filesArr[i].name + ' <span class="fa fa-times-circle fa-lg closeBtn" title="remove"></span></div>');
}
}
function addFiles(e) {
var tmp;
// transfer dropped content to temporary array
if (e.dataTransfer) {
tmp = e.dataTransfer.files;
} else if (e.target) {
tmp = e.target.files;
}
// Copy the file items into the array
for(var i = 0; i < tmp.length; i++) {
filesArr.push(tmp.item(i));
}
// remove all contents from the input elemnent (reset it)
inputFile.wrap('<form>').closest('form').get(0).reset();
inputFile.unwrap();
showFiles();
}
document.getElementById(dropZoneId).addEventListener("dragover", function (e) {
e.preventDefault();
e.stopPropagation();
dropZone.addClass(mouseOverClass);
var x = e.pageX;
var y = e.pageY;
if (!(x < ooleft || x > ooright || y < ootop || y > oobottom)) {
inputFile.offset({
top: y - 15,
left: x - 100
});
} else {
inputFile.offset({
top: -400,
left: -400
});
}
}, true);
if (buttonId != "") {
var clickZone = $("#" + buttonId);
var oleft = clickZone.offset().left;
var oright = clickZone.outerWidth() + oleft;
var otop = clickZone.offset().top;
var obottom = clickZone.outerHeight() + otop;
$("#" + buttonId).mousemove(function (e) {
var x = e.pageX;
var y = e.pageY;
if (!(x < oleft || x > oright || y < otop || y > obottom)) {
inputFile.offset({
top: y - 15,
left: x - 160
});
} else {
inputFile.offset({
top: -400,
left: -400
});
}
});
}
document.getElementById(dropZoneId).addEventListener("drop", function (e) {
$("#" + dropZoneId).removeClass(mouseOverClass);
addFiles(e);
}, true);
inputFile.on('change', function(e) {
addFiles(e);
});
$('#filename').on('click', '.closeBtn', function(e) {
e.preventDefault();
e.stopPropagation();
var divElem = $(this).parent();
var index = $('#filename').find('div').index(divElem);
if ( index !== -1 ) {
$('#filename')[0].removeChild(divElem[0]);
filesArr.slice(index,1);
}
});
})
由于我们无法修改 <input type'file' multiple>
标记中的所选文件数组,因此我已更新您的代码以显示文件计数并在选择多个文件时删除所有文件。
有一个fiddle的updated code。
$(function () {
var dropZoneId = "drop-zone";
var buttonId = "clickHere";
var mouseOverClass = "mouse-over";
var dropZone = $("#" + dropZoneId);
var ooleft = dropZone.offset().left;
var ooright = dropZone.outerWidth() + ooleft;
var ootop = dropZone.offset().top;
var oobottom = dropZone.outerHeight() + ootop;
var inputFile = dropZone.find("input");
document.getElementById(dropZoneId).addEventListener("dragover", function (e) {
e.preventDefault();
e.stopPropagation();
dropZone.addClass(mouseOverClass);
var x = e.pageX;
var y = e.pageY;
if (!(x < ooleft || x > ooright || y < ootop || y > oobottom)) {
inputFile.offset({
top: y - 15,
left: x - 100
});
} else {
inputFile.offset({
top: -400,
left: -400
});
}
}, true);
if (buttonId != "") {
var clickZone = $("#" + buttonId);
var oleft = clickZone.offset().left;
var oright = clickZone.outerWidth() + oleft;
var otop = clickZone.offset().top;
var obottom = clickZone.outerHeight() + otop;
$("#" + buttonId).mousemove(function (e) {
var x = e.pageX;
var y = e.pageY;
if (!(x < oleft || x > oright || y < otop || y > obottom)) {
inputFile.offset({
top: y - 15,
left: x - 160
});
} else {
inputFile.offset({
top: -400,
left: -400
});
}
});
}
document.getElementById(dropZoneId).addEventListener("drop", function (e) {
$("#" + dropZoneId).removeClass(mouseOverClass);
}, true);
inputFile.on('change', function (e) {
$('#filename').html("");
var fileNum = this.files.length,
initial = 0,
counter = 0,
fileNames = "";
for (initial; initial < fileNum; initial++) {
counter = counter + 1;
fileNames += this.files[initial].name + ' ';
}
if(fileNum > 1)
fileNames = 'Files selected...';
else
fileNames = this.files[0].name + ' ';
$('#filename').append('<span class="fa-stack fa-lg"><i class="fa fa-file fa-stack-1x "></i><strong class="fa-stack-1x" style="color:#FFF; font-size:12px; margin-top:2px;">'+ fileNum + '</strong></span><span">' + fileNames + '</span> <span class="fa fa-times-circle fa-lg closeBtn" title="remove"></span><br>');
// add remove event
$('#filename').find('.closeBtn').click(function(){
$('#filename').empty();
inputFile.val('');
});
///End change
});
})
HTML5 文件输入的文件列表是只读的,因此当您尝试从中删除文件时将无法获得许可。
您需要做的是维护一个单独的数组列表(JSON 数组,如示例所示)。
我用 div 包装了你的 X 按钮,它保存文件索引并连接到 'file_' 字符串,并添加了一个 onclick
函数 removeLine(obj)
接受元素作为对象。
我还在全局范围内添加了一个 JSON 数组 finalFiles
,并将 inputFile
移到了全局范围内。
当文件输入发生变化时,我通过 :
将所选文件设置为 JSON 数组
$.each(this.files,function(idx,elm){
finalFiles[idx]=elm;
});
函数removeLine
如果用户误删除了文件,函数removeLine
将刷新输入文件列表以允许再次选择相同的文件,函数从包装器division id获取文件索引, 删除包装器 div 然后从 JSON 数组中删除文件。
function removeLine(obj)
{
inputFile.val('');
var jqObj = $(obj);
var container = jqObj.closest('div');
var index = container.attr("id").split('_')[1];
container.remove();
delete finalFiles[index];
//console.log(finalFiles);
}
您可以在表单提交时维护您的文件,并以类似于 This Article.
的方式使用 FormData 通过 AJAX post 发送它们
var dropZoneId = "drop-zone";
var buttonId = "clickHere";
var mouseOverClass = "mouse-over";
var dropZone = $("#" + dropZoneId);
var inputFile = dropZone.find("input");
var finalFiles = {};
$(function() {
var ooleft = dropZone.offset().left;
var ooright = dropZone.outerWidth() + ooleft;
var ootop = dropZone.offset().top;
var oobottom = dropZone.outerHeight() + ootop;
document.getElementById(dropZoneId).addEventListener("dragover", function(e) {
e.preventDefault();
e.stopPropagation();
dropZone.addClass(mouseOverClass);
var x = e.pageX;
var y = e.pageY;
if (!(x < ooleft || x > ooright || y < ootop || y > oobottom)) {
inputFile.offset({
top: y - 15,
left: x - 100
});
} else {
inputFile.offset({
top: -400,
left: -400
});
}
}, true);
if (buttonId != "") {
var clickZone = $("#" + buttonId);
var oleft = clickZone.offset().left;
var oright = clickZone.outerWidth() + oleft;
var otop = clickZone.offset().top;
var obottom = clickZone.outerHeight() + otop;
$("#" + buttonId).mousemove(function(e) {
var x = e.pageX;
var y = e.pageY;
if (!(x < oleft || x > oright || y < otop || y > obottom)) {
inputFile.offset({
top: y - 15,
left: x - 160
});
} else {
inputFile.offset({
top: -400,
left: -400
});
}
});
}
document.getElementById(dropZoneId).addEventListener("drop", function(e) {
$("#" + dropZoneId).removeClass(mouseOverClass);
}, true);
inputFile.on('change', function(e) {
finalFiles = {};
$('#filename').html("");
var fileNum = this.files.length,
initial = 0,
counter = 0;
$.each(this.files,function(idx,elm){
finalFiles[idx]=elm;
});
for (initial; initial < fileNum; initial++) {
counter = counter + 1;
$('#filename').append('<div id="file_'+ initial +'"><span class="fa-stack fa-lg"><i class="fa fa-file fa-stack-1x "></i><strong class="fa-stack-1x" style="color:#FFF; font-size:12px; margin-top:2px;">' + counter + '</strong></span> ' + this.files[initial].name + ' <span class="fa fa-times-circle fa-lg closeBtn" onclick="removeLine(this)" title="remove"></span></div>');
}
});
})
function removeLine(obj)
{
inputFile.val('');
var jqObj = $(obj);
var container = jqObj.closest('div');
var index = container.attr("id").split('_')[1];
container.remove();
delete finalFiles[index];
//console.log(finalFiles);
}
#drop-zone {
width: 100%;
min-height: 150px;
border: 3px dashed rgba(0, 0, 0, .3);
border-radius: 5px;
font-family: Arial;
text-align: center;
position: relative;
font-size: 20px;
color: #7E7E7E;
}
#drop-zone input {
position: absolute;
cursor: pointer;
left: 0px;
top: 0px;
opacity: 0;
}
/*Important*/
#drop-zone.mouse-over {
border: 3px dashed rgba(0, 0, 0, .3);
color: #7E7E7E;
}
/*If you dont want the button*/
#clickHere {
display: inline-block;
cursor: pointer;
color: white;
font-size: 17px;
width: 150px;
border-radius: 4px;
background-color: #4679BD;
padding: 10px;
}
#clickHere:hover {
background-color: #376199;
}
#filename {
margin-top: 10px;
margin-bottom: 10px;
font-size: 14px;
line-height: 1.5em;
}
.file-preview {
background: #ccc;
border: 5px solid #fff;
box-shadow: 0 0 4px rgba(0, 0, 0, 0.5);
display: inline-block;
width: 60px;
height: 60px;
text-align: center;
font-size: 14px;
margin-top: 5px;
}
.closeBtn:hover {
color: red;
display:inline-block;
}
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<div id="drop-zone">
<p>Drop files here...</p>
<div id="clickHere">or click here.. <i class="fa fa-upload"></i>
<input type="file" name="file" id="file" multiple />
</div>
<div id='filename'></div>
</div>
$(function () {
var dropZoneId = "drop-zone";
var buttonId = "clickHere";
var mouseOverClass = "mouse-over";
var dropZone = $("#" + dropZoneId);
var ooleft = dropZone.offset().left;
var ooright = dropZone.outerWidth() + ooleft;
var ootop = dropZone.offset().top;
var oobottom = dropZone.outerHeight() + ootop;
var inputFile = dropZone.find("input");
var filesArr = [];
function showFiles() {
$('#filename').html("");
var fileNum = filesArr.length;
for (var i = 0; i < fileNum; i++) {
objectURL = URL.createObjectURL(filesArr[i]);
$('#filename').append('<div><img title="'+filesArr[i].name+'" id="'+objectURL+'" src="'+objectURL+'" class="pre-visualizacao"><span class="fa-stack fa-lg"><i class="fa fa-file fa-stack-1x "></i><strong class="fa-stack-1x" style="color:#FFF; font-size:12px; margin-top:2px;">'+ i + '</strong></span> ' + filesArr[i].name + ' <span class="closeBtn" title="Remover">X</span></div>');
}
}
function addFiles(e) {
var tmp;
// transfer dropped content to temporary array
if (e.dataTransfer) {
tmp = e.dataTransfer.files;
} else if (e.target) {
tmp = e.target.files;
}
// Copy the file items into the array
for(var i = 0; i < tmp.length; i++) {
filesArr.push(tmp.item(i));
//console.log(i);
}
// remove all contents from the input elemnent (reset it)
inputFile.wrap('<form>').closest('form').get(0).reset();
inputFile.unwrap();
showFiles();
}
document.getElementById(dropZoneId).addEventListener("dragover", function (e) {
e.preventDefault();
e.stopPropagation();
dropZone.addClass(mouseOverClass);
var x = e.pageX;
var y = e.pageY;
if (!(x < ooleft || x > ooright || y < ootop || y > oobottom)) {
inputFile.offset({
top: y - 15,
left: x - 100
});
} else {
inputFile.offset({
top: -400,
left: -400
});
}
}, true);
if (buttonId != "") {
var clickZone = $("#" + buttonId);
var oleft = clickZone.offset().left;
var oright = clickZone.outerWidth() + oleft;
var otop = clickZone.offset().top;
var obottom = clickZone.outerHeight() + otop;
$("#" + buttonId).mousemove(function (e) {
var x = e.pageX;
var y = e.pageY;
if (!(x < oleft || x > oright || y < otop || y > obottom)) {
inputFile.offset({
top: y - 15,
left: x - 160
});
} else {
inputFile.offset({
top: -400,
left: -400
});
}
});
}
document.getElementById(dropZoneId).addEventListener("drop", function (e) {
$("#" + dropZoneId).removeClass(mouseOverClass);
addFiles(e);
}, true);
/*inputFile.on('change', function(e) {
addFiles(e);
});*/
$('#filename').on('click', '.closeBtn', function(e) {
e.preventDefault();
e.stopPropagation();
var divElem = $(this).parent();
var index = $('#filename').find('div').index(divElem);
if ( index !== -1 ) {
$('#filename')[0].removeChild(divElem[0]);
filesArr.slice(index,1);
}
});
inputFile.on('change', function(e) {
$('#filename').html("");
var fileNum = this.files.length,
initial = 0,
counter = 0;
for (initial; initial < fileNum; initial++) {
counter = counter + 1;
objectURL = URL.createObjectURL(this.files[initial]);
$('#filename').append('<div><img title="'+this.files[initial].name+'" id="'+objectURL+'" src="'+objectURL+'" class="pre-visualizacao"><span class="fa-stack fa-lg"><i class="fa fa-file fa-stack-1x "></i><strong class="fa-stack-1x" style="color:#FFF; font-size:12px; margin-top:2px;">'+ counter + '</strong></span> ' + this.files[initial].name + ' <span class="closeBtn" title="Remover">X</span></div>');
}
});
});
如何在此处添加删除按钮,就像这样在文件队列中一个一个地删除
我之所以不使用带 OOB 插件的免费文件上传插件,是因为我的客户要求uirements 是出于安全目的,他们需要简单的上传 ui,没有任何复杂的插件。
$(function() {
var dropZoneId = "drop-zone";
var buttonId = "clickHere";
var mouseOverClass = "mouse-over";
var dropZone = $("#" + dropZoneId);
var ooleft = dropZone.offset().left;
var ooright = dropZone.outerWidth() + ooleft;
var ootop = dropZone.offset().top;
var oobottom = dropZone.outerHeight() + ootop;
var inputFile = dropZone.find("input");
document.getElementById(dropZoneId).addEventListener("dragover", function(e) {
e.preventDefault();
e.stopPropagation();
dropZone.addClass(mouseOverClass);
var x = e.pageX;
var y = e.pageY;
if (!(x < ooleft || x > ooright || y < ootop || y > oobottom)) {
inputFile.offset({
top: y - 15,
left: x - 100
});
} else {
inputFile.offset({
top: -400,
left: -400
});
}
}, true);
if (buttonId != "") {
var clickZone = $("#" + buttonId);
var oleft = clickZone.offset().left;
var oright = clickZone.outerWidth() + oleft;
var otop = clickZone.offset().top;
var obottom = clickZone.outerHeight() + otop;
$("#" + buttonId).mousemove(function(e) {
var x = e.pageX;
var y = e.pageY;
if (!(x < oleft || x > oright || y < otop || y > obottom)) {
inputFile.offset({
top: y - 15,
left: x - 160
});
} else {
inputFile.offset({
top: -400,
left: -400
});
}
});
}
document.getElementById(dropZoneId).addEventListener("drop", function(e) {
$("#" + dropZoneId).removeClass(mouseOverClass);
}, true);
inputFile.on('change', function(e) {
$('#filename').html("");
var fileNum = this.files.length,
initial = 0,
counter = 0;
for (initial; initial < fileNum; initial++) {
counter = counter + 1;
$('#filename').append('<span class="fa-stack fa-lg"><i class="fa fa-file fa-stack-1x "></i><strong class="fa-stack-1x" style="color:#FFF; font-size:12px; margin-top:2px;">' + counter + '</strong></span> ' + this.files[initial].name + ' <span class="fa fa-times-circle fa-lg closeBtn" title="remove"></span><br>');
}
});
})
#drop-zone {
width: 100%;
min-height: 150px;
border: 3px dashed rgba(0, 0, 0, .3);
border-radius: 5px;
font-family: Arial;
text-align: center;
position: relative;
font-size: 20px;
color: #7E7E7E;
}
#drop-zone input {
position: absolute;
cursor: pointer;
left: 0px;
top: 0px;
opacity: 0;
}
/*Important*/
#drop-zone.mouse-over {
border: 3px dashed rgba(0, 0, 0, .3);
color: #7E7E7E;
}
/*If you dont want the button*/
#clickHere {
display: inline-block;
cursor: pointer;
color: white;
font-size: 17px;
width: 150px;
border-radius: 4px;
background-color: #4679BD;
padding: 10px;
}
#clickHere:hover {
background-color: #376199;
}
#filename {
margin-top: 10px;
margin-bottom: 10px;
font-size: 14px;
line-height: 1.5em;
}
.file-preview {
background: #ccc;
border: 5px solid #fff;
box-shadow: 0 0 4px rgba(0, 0, 0, 0.5);
display: inline-block;
width: 60px;
height: 60px;
text-align: center;
font-size: 14px;
margin-top: 5px;
}
.closeBtn:hover {
color: red;
}
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<div id="drop-zone">
<p>Drop files here...</p>
<div id="clickHere">or click here.. <i class="fa fa-upload"></i>
<input type="file" name="file" id="file" multiple />
</div>
<div id='filename'></div>
</div>
注意:我不拥有代码,我已经将它作为我的资源从其他人那里重新使用并为我的客户修改了它
**UPDATE Here my fiddle link
我以前为我的 Dropzone 做过这个。随意调整。这是来自我的 Laravel 应用程序。你应该关注 avatar_refresh_upload
。剪掉不必要的东西就大功告成了。
function avatar_refresh_upload() {
var input = $('input#avatar[type=file]');
input.replaceWith(input.val('').clone(true));
$('#selected_file').html('{{ Lang::get('app.profile_avatar_select') }}');
$('#avatar_refresh_upload').removeAttr('style');
}
$(document).ready(function ($) {
$('input:file#avatar').change(function () {
var file_name = $(this).val();
if (file_name.length > 10) {
file_name = file_name.substring(0, 10) + '...';
}
$('#selected_file').html('File "' + file_name + '" chosen');
$('#avatar_refresh_upload').css('display', 'inline-block');
});
$('#avatar_refresh_upload').on('click', function () {
avatar_refresh_upload();
});
@if ($user->avatar != '')
$('#remove_avatar').change(function () {
if ($(this).is(':checked')) {
avatar_refresh_upload();
$('#avatar').prop('disabled', true);
$('#avatar_preview').css('opacity', '0.5');
$('#avatar_upload_form_area').css('opacity', '0.5');
$('#remove_avatar_info').show();
} else {
$('#avatar').prop('disabled', false);
$('#avatar_preview').removeAttr('style');
$('#avatar_upload_form_area').removeAttr('style');
$('#remove_avatar_info').removeAttr('style');
}
});
@endif
});
长话短说 - 如果您想在选择要上传的文件后但在提交之前重置输入文件,则必须 运行:
input.replaceWith(input.val('').clone(true));
$(function () {
var dropZoneId = "drop-zone";
var buttonId = "clickHere";
var mouseOverClass = "mouse-over";
var dropZone = $("#" + dropZoneId);
var ooleft = dropZone.offset().left;
var ooright = dropZone.outerWidth() + ooleft;
var ootop = dropZone.offset().top;
var oobottom = dropZone.outerHeight() + ootop;
var inputFile = dropZone.find("input");
var filesArr = [];
function showFiles() {
$('#filename').html("");
var fileNum = filesArr.length;
for (var i = 0; i < fileNum; i++) {
$('#filename').append('<div><span class="fa-stack fa-lg"><i class="fa fa-file fa-stack-1x "></i><strong class="fa-stack-1x" style="color:#FFF; font-size:12px; margin-top:2px;">'+ i + '</strong></span> ' + filesArr[i].name + ' <span class="fa fa-times-circle fa-lg closeBtn" title="remove"></span></div>');
}
}
function addFiles(e) {
var tmp;
// transfer dropped content to temporary array
if (e.dataTransfer) {
tmp = e.dataTransfer.files;
} else if (e.target) {
tmp = e.target.files;
}
// Copy the file items into the array
for(var i = 0; i < tmp.length; i++) {
filesArr.push(tmp.item(i));
}
// remove all contents from the input elemnent (reset it)
inputFile.wrap('<form>').closest('form').get(0).reset();
inputFile.unwrap();
showFiles();
}
document.getElementById(dropZoneId).addEventListener("dragover", function (e) {
e.preventDefault();
e.stopPropagation();
dropZone.addClass(mouseOverClass);
var x = e.pageX;
var y = e.pageY;
if (!(x < ooleft || x > ooright || y < ootop || y > oobottom)) {
inputFile.offset({
top: y - 15,
left: x - 100
});
} else {
inputFile.offset({
top: -400,
left: -400
});
}
}, true);
if (buttonId != "") {
var clickZone = $("#" + buttonId);
var oleft = clickZone.offset().left;
var oright = clickZone.outerWidth() + oleft;
var otop = clickZone.offset().top;
var obottom = clickZone.outerHeight() + otop;
$("#" + buttonId).mousemove(function (e) {
var x = e.pageX;
var y = e.pageY;
if (!(x < oleft || x > oright || y < otop || y > obottom)) {
inputFile.offset({
top: y - 15,
left: x - 160
});
} else {
inputFile.offset({
top: -400,
left: -400
});
}
});
}
document.getElementById(dropZoneId).addEventListener("drop", function (e) {
$("#" + dropZoneId).removeClass(mouseOverClass);
addFiles(e);
}, true);
inputFile.on('change', function(e) {
addFiles(e);
});
$('#filename').on('click', '.closeBtn', function(e) {
e.preventDefault();
e.stopPropagation();
var divElem = $(this).parent();
var index = $('#filename').find('div').index(divElem);
if ( index !== -1 ) {
$('#filename')[0].removeChild(divElem[0]);
filesArr.slice(index,1);
}
});
})
由于我们无法修改 <input type'file' multiple>
标记中的所选文件数组,因此我已更新您的代码以显示文件计数并在选择多个文件时删除所有文件。
有一个fiddle的updated code。
$(function () {
var dropZoneId = "drop-zone";
var buttonId = "clickHere";
var mouseOverClass = "mouse-over";
var dropZone = $("#" + dropZoneId);
var ooleft = dropZone.offset().left;
var ooright = dropZone.outerWidth() + ooleft;
var ootop = dropZone.offset().top;
var oobottom = dropZone.outerHeight() + ootop;
var inputFile = dropZone.find("input");
document.getElementById(dropZoneId).addEventListener("dragover", function (e) {
e.preventDefault();
e.stopPropagation();
dropZone.addClass(mouseOverClass);
var x = e.pageX;
var y = e.pageY;
if (!(x < ooleft || x > ooright || y < ootop || y > oobottom)) {
inputFile.offset({
top: y - 15,
left: x - 100
});
} else {
inputFile.offset({
top: -400,
left: -400
});
}
}, true);
if (buttonId != "") {
var clickZone = $("#" + buttonId);
var oleft = clickZone.offset().left;
var oright = clickZone.outerWidth() + oleft;
var otop = clickZone.offset().top;
var obottom = clickZone.outerHeight() + otop;
$("#" + buttonId).mousemove(function (e) {
var x = e.pageX;
var y = e.pageY;
if (!(x < oleft || x > oright || y < otop || y > obottom)) {
inputFile.offset({
top: y - 15,
left: x - 160
});
} else {
inputFile.offset({
top: -400,
left: -400
});
}
});
}
document.getElementById(dropZoneId).addEventListener("drop", function (e) {
$("#" + dropZoneId).removeClass(mouseOverClass);
}, true);
inputFile.on('change', function (e) {
$('#filename').html("");
var fileNum = this.files.length,
initial = 0,
counter = 0,
fileNames = "";
for (initial; initial < fileNum; initial++) {
counter = counter + 1;
fileNames += this.files[initial].name + ' ';
}
if(fileNum > 1)
fileNames = 'Files selected...';
else
fileNames = this.files[0].name + ' ';
$('#filename').append('<span class="fa-stack fa-lg"><i class="fa fa-file fa-stack-1x "></i><strong class="fa-stack-1x" style="color:#FFF; font-size:12px; margin-top:2px;">'+ fileNum + '</strong></span><span">' + fileNames + '</span> <span class="fa fa-times-circle fa-lg closeBtn" title="remove"></span><br>');
// add remove event
$('#filename').find('.closeBtn').click(function(){
$('#filename').empty();
inputFile.val('');
});
///End change
});
})
HTML5 文件输入的文件列表是只读的,因此当您尝试从中删除文件时将无法获得许可。
您需要做的是维护一个单独的数组列表(JSON 数组,如示例所示)。
我用 div 包装了你的 X 按钮,它保存文件索引并连接到 'file_' 字符串,并添加了一个 onclick
函数 removeLine(obj)
接受元素作为对象。
我还在全局范围内添加了一个 JSON 数组 finalFiles
,并将 inputFile
移到了全局范围内。
当文件输入发生变化时,我通过 :
将所选文件设置为 JSON 数组$.each(this.files,function(idx,elm){
finalFiles[idx]=elm;
});
函数removeLine
如果用户误删除了文件,函数removeLine
将刷新输入文件列表以允许再次选择相同的文件,函数从包装器division id获取文件索引, 删除包装器 div 然后从 JSON 数组中删除文件。
function removeLine(obj)
{
inputFile.val('');
var jqObj = $(obj);
var container = jqObj.closest('div');
var index = container.attr("id").split('_')[1];
container.remove();
delete finalFiles[index];
//console.log(finalFiles);
}
您可以在表单提交时维护您的文件,并以类似于 This Article.
的方式使用 FormData 通过 AJAX post 发送它们var dropZoneId = "drop-zone";
var buttonId = "clickHere";
var mouseOverClass = "mouse-over";
var dropZone = $("#" + dropZoneId);
var inputFile = dropZone.find("input");
var finalFiles = {};
$(function() {
var ooleft = dropZone.offset().left;
var ooright = dropZone.outerWidth() + ooleft;
var ootop = dropZone.offset().top;
var oobottom = dropZone.outerHeight() + ootop;
document.getElementById(dropZoneId).addEventListener("dragover", function(e) {
e.preventDefault();
e.stopPropagation();
dropZone.addClass(mouseOverClass);
var x = e.pageX;
var y = e.pageY;
if (!(x < ooleft || x > ooright || y < ootop || y > oobottom)) {
inputFile.offset({
top: y - 15,
left: x - 100
});
} else {
inputFile.offset({
top: -400,
left: -400
});
}
}, true);
if (buttonId != "") {
var clickZone = $("#" + buttonId);
var oleft = clickZone.offset().left;
var oright = clickZone.outerWidth() + oleft;
var otop = clickZone.offset().top;
var obottom = clickZone.outerHeight() + otop;
$("#" + buttonId).mousemove(function(e) {
var x = e.pageX;
var y = e.pageY;
if (!(x < oleft || x > oright || y < otop || y > obottom)) {
inputFile.offset({
top: y - 15,
left: x - 160
});
} else {
inputFile.offset({
top: -400,
left: -400
});
}
});
}
document.getElementById(dropZoneId).addEventListener("drop", function(e) {
$("#" + dropZoneId).removeClass(mouseOverClass);
}, true);
inputFile.on('change', function(e) {
finalFiles = {};
$('#filename').html("");
var fileNum = this.files.length,
initial = 0,
counter = 0;
$.each(this.files,function(idx,elm){
finalFiles[idx]=elm;
});
for (initial; initial < fileNum; initial++) {
counter = counter + 1;
$('#filename').append('<div id="file_'+ initial +'"><span class="fa-stack fa-lg"><i class="fa fa-file fa-stack-1x "></i><strong class="fa-stack-1x" style="color:#FFF; font-size:12px; margin-top:2px;">' + counter + '</strong></span> ' + this.files[initial].name + ' <span class="fa fa-times-circle fa-lg closeBtn" onclick="removeLine(this)" title="remove"></span></div>');
}
});
})
function removeLine(obj)
{
inputFile.val('');
var jqObj = $(obj);
var container = jqObj.closest('div');
var index = container.attr("id").split('_')[1];
container.remove();
delete finalFiles[index];
//console.log(finalFiles);
}
#drop-zone {
width: 100%;
min-height: 150px;
border: 3px dashed rgba(0, 0, 0, .3);
border-radius: 5px;
font-family: Arial;
text-align: center;
position: relative;
font-size: 20px;
color: #7E7E7E;
}
#drop-zone input {
position: absolute;
cursor: pointer;
left: 0px;
top: 0px;
opacity: 0;
}
/*Important*/
#drop-zone.mouse-over {
border: 3px dashed rgba(0, 0, 0, .3);
color: #7E7E7E;
}
/*If you dont want the button*/
#clickHere {
display: inline-block;
cursor: pointer;
color: white;
font-size: 17px;
width: 150px;
border-radius: 4px;
background-color: #4679BD;
padding: 10px;
}
#clickHere:hover {
background-color: #376199;
}
#filename {
margin-top: 10px;
margin-bottom: 10px;
font-size: 14px;
line-height: 1.5em;
}
.file-preview {
background: #ccc;
border: 5px solid #fff;
box-shadow: 0 0 4px rgba(0, 0, 0, 0.5);
display: inline-block;
width: 60px;
height: 60px;
text-align: center;
font-size: 14px;
margin-top: 5px;
}
.closeBtn:hover {
color: red;
display:inline-block;
}
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<div id="drop-zone">
<p>Drop files here...</p>
<div id="clickHere">or click here.. <i class="fa fa-upload"></i>
<input type="file" name="file" id="file" multiple />
</div>
<div id='filename'></div>
</div>
$(function () {
var dropZoneId = "drop-zone";
var buttonId = "clickHere";
var mouseOverClass = "mouse-over";
var dropZone = $("#" + dropZoneId);
var ooleft = dropZone.offset().left;
var ooright = dropZone.outerWidth() + ooleft;
var ootop = dropZone.offset().top;
var oobottom = dropZone.outerHeight() + ootop;
var inputFile = dropZone.find("input");
var filesArr = [];
function showFiles() {
$('#filename').html("");
var fileNum = filesArr.length;
for (var i = 0; i < fileNum; i++) {
objectURL = URL.createObjectURL(filesArr[i]);
$('#filename').append('<div><img title="'+filesArr[i].name+'" id="'+objectURL+'" src="'+objectURL+'" class="pre-visualizacao"><span class="fa-stack fa-lg"><i class="fa fa-file fa-stack-1x "></i><strong class="fa-stack-1x" style="color:#FFF; font-size:12px; margin-top:2px;">'+ i + '</strong></span> ' + filesArr[i].name + ' <span class="closeBtn" title="Remover">X</span></div>');
}
}
function addFiles(e) {
var tmp;
// transfer dropped content to temporary array
if (e.dataTransfer) {
tmp = e.dataTransfer.files;
} else if (e.target) {
tmp = e.target.files;
}
// Copy the file items into the array
for(var i = 0; i < tmp.length; i++) {
filesArr.push(tmp.item(i));
//console.log(i);
}
// remove all contents from the input elemnent (reset it)
inputFile.wrap('<form>').closest('form').get(0).reset();
inputFile.unwrap();
showFiles();
}
document.getElementById(dropZoneId).addEventListener("dragover", function (e) {
e.preventDefault();
e.stopPropagation();
dropZone.addClass(mouseOverClass);
var x = e.pageX;
var y = e.pageY;
if (!(x < ooleft || x > ooright || y < ootop || y > oobottom)) {
inputFile.offset({
top: y - 15,
left: x - 100
});
} else {
inputFile.offset({
top: -400,
left: -400
});
}
}, true);
if (buttonId != "") {
var clickZone = $("#" + buttonId);
var oleft = clickZone.offset().left;
var oright = clickZone.outerWidth() + oleft;
var otop = clickZone.offset().top;
var obottom = clickZone.outerHeight() + otop;
$("#" + buttonId).mousemove(function (e) {
var x = e.pageX;
var y = e.pageY;
if (!(x < oleft || x > oright || y < otop || y > obottom)) {
inputFile.offset({
top: y - 15,
left: x - 160
});
} else {
inputFile.offset({
top: -400,
left: -400
});
}
});
}
document.getElementById(dropZoneId).addEventListener("drop", function (e) {
$("#" + dropZoneId).removeClass(mouseOverClass);
addFiles(e);
}, true);
/*inputFile.on('change', function(e) {
addFiles(e);
});*/
$('#filename').on('click', '.closeBtn', function(e) {
e.preventDefault();
e.stopPropagation();
var divElem = $(this).parent();
var index = $('#filename').find('div').index(divElem);
if ( index !== -1 ) {
$('#filename')[0].removeChild(divElem[0]);
filesArr.slice(index,1);
}
});
inputFile.on('change', function(e) {
$('#filename').html("");
var fileNum = this.files.length,
initial = 0,
counter = 0;
for (initial; initial < fileNum; initial++) {
counter = counter + 1;
objectURL = URL.createObjectURL(this.files[initial]);
$('#filename').append('<div><img title="'+this.files[initial].name+'" id="'+objectURL+'" src="'+objectURL+'" class="pre-visualizacao"><span class="fa-stack fa-lg"><i class="fa fa-file fa-stack-1x "></i><strong class="fa-stack-1x" style="color:#FFF; font-size:12px; margin-top:2px;">'+ counter + '</strong></span> ' + this.files[initial].name + ' <span class="closeBtn" title="Remover">X</span></div>');
}
});
});