如何检查我的输入文件上传副本?

时间:2016-06-19 03:33:56

标签: javascript jquery duplicates multiple-file-upload

我有这个表单,它有一个文件上传按钮。当您选择一个文件时,它会在upload_prev div中显示。 我的问题是,当我尝试选择相同的文件时,没有任何反应。我想要验证或运行非复制功能。 我做到了我尝试了许多方法和方法,比如使用子节点并查看内部文本是否相同。我尝试使用jquery循环并获取值,但所有这些都失败了。我想在我再次选择它时显示一条消息,说明该文件已经在upload_prev的Box中。

<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">

  <script type="text/javascript" src="https://code.jquery.com/jquery-1.10.1.js"></script>

      <link rel="stylesheet" type="text/css" href="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.min.css">

      <link rel="stylesheet" type="text/css" href="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.min.css">

  <style type="text/css">
    .fileUpload {
  position: relative;
  overflow: hidden;
  margin: 10px;
}

.fileUpload input.upload {
  position: absolute;
  top: 0;
  right: 0;
  margin: 0;
  padding: 0;
  font-size: 20px;
  cursor: pointer;
  opacity: 0;
  background-color: #fff;
  filter: alpha(opacity=0);
}

.buttonwrap {
  text-align: center;
  padding-top: 20px;
  float: left;
  display: block;
}

.buttonsend:hover {
  background-color: rgba(255, 255, 255, 0.2);
  color: #225C51;
}

.buttonsend {
  text-decoration: none;
  color: #fff;
  font-size: 18px;
  padding-top: 5px;
  padding-bottom: 5px;
  padding-left: 10px;
  padding-right: 10px;
  background-color: rgba(72, 133, 130, .5);
}

span {
  float: left;
  display: flex;
  width: 100%;
}

p.closed {
  margin: 0 0 0 7px;
  cursor: pointer;
}

  </style>

  <title></title>

<script type='text/javascript'>//<![CDATA[

$(window).load(function(){  
//  TO CLOSE THE SLECTED FILE
$(document).on('click', '.close', function() {
  $(this).parents('span').remove();
})

//JUST TO PUT A VALUE IN THE BOX WHICH HAS 
document.getElementById("uploadBtn").onchange = function() {
  document.getElementById("uploadFile").value = this.value;
};

document.getElementById('uploadBtn').onchange = uploadOnChange;
//document.getElementById('uploadBtn').onchange = myFunction;


function uploadOnChange() {

  var filename = this.value;
  var lastIndex = filename.lastIndexOf("\\");
  if (lastIndex >= 0) {
    filename = filename.substring(lastIndex + 1);
  }


//  alert (filename);

  var files = $('#uploadBtn')[0].files;

  for (var i = 0; i < files.length; i++) {
    $("#upload_prev").append('<span>' + '<div class="hesh">' + files[i].name +'</div>' + '<p class="close">X</p></span>');
  }
  document.getElementById('filename').value = filename;

        document.getElementById("demo").innerHTML = files.length;

}


});//]]> 

</script>


</head>

<body>
  <FORM METHOD="post" ACTION="" ENCTYPE="multipart/form-data">
<!--  <input id="uploadFile" placeholder="Add files from My Computer" class="steptextboxq3" />-->
  <div class="fileUpload btn btn-primary">
    <span>Browse</span>
    <input id="uploadBtn" type="file" class="upload" multiple="multiple" name="browsefile" />
  </div>
  <input id="filename" type="text" />

  <div id="upload_prev"></div>


  <div style="clear:both;"></div>
  <div class="buttonwrap">
    <a href="contactus.html" class="buttonsend" style="float:right;">Send </a> </div>
</FORM>

    <p id="demo"></p>

</body>

</html>

这是我的小提琴。我怎样才能找到办法。

https://jsfiddle.net/Lc5gb7c9/

3 个答案:

答案 0 :(得分:4)

您可以创建一个存储files[i].name的数组,使用Array.prototype.indexOf()检查文件名是否已添加到数组中,如果true调用alert(),则添加文件名到数组。您可以在处理过程中的任何时刻将数组重置为[]

注意,<div><p>元素不是<span>元素的有效内容

// outside of `onchange`
var arr = [];

for (var i = 0; i < files.length; i++) {
  if (arr.indexOf(files[i].name) === -1) { 
  arr.push(files[i].name)
    $("#upload_prev").append('<div>' 
     + '<div class="hesh">' 
     + files[i].name + '</div>' 
     + '<p class="close">X</p></div>');
  } else {
    alert(files[i].name + " already selected")
  }
}

jsfiddle https://jsfiddle.net/Lc5gb7c9/2/

答案 1 :(得分:0)

我认为您遇到了将文件分配到dom的问题。请记住FileLists是只读的,这意味着您无法选择多个文件,然后继续将它们附加到现有元素。

但是你可以将它们附加到JavaScript数组中:

files=[]; files.push(fileList);

所以,我已经编辑了你的JSFiddle以包含这个功能,以及你要求的支票:

https://jsfiddle.net/Lc5gb7c9/3/

我建议你看看: http://blog.teamtreehouse.com/uploading-files-ajax表示通过Ajax上传的方式,因为您需要创建formData对象,然后遍历您的uploaded_files数组并将它们附加到正确的formData键。他们从html元素获取文件,但你已经在uploaded_files数组中有文件,所以你会这样做:

 for (var i = 0; i < uploaded_files.length; i++) {
       formData.append('files[]', uploaded_files[i], uploaded_files[i].name);
 }

完成后,您可以拨打电话。

答案 2 :(得分:0)

具有相同名称,大小,类型,修改时间的文件重复并具有不同内容的可能性很小

const existingFiles = []

function findFile(file) {
  return existingFiles.find(function(existingFile) {
    return (
      existingFile.name         === file.name &&
      existingFile.lastModified === file.lastModified &&
      existingFile.size         === file.size &&
      existingFile.type         === file.type
    )
  })
}

const input = document.querySelector('input')

input.addEventListener('change', function(e) {
  const files = e.target.files
  Array.from(files).forEach(function(file) {
    const existingFile = findFile(file)
    
    if (existingFile) {
      console.error('Existing file: ', existingFile)
      return
    }
    
    existingFiles.push(file)
    console.warn('Added file: ', file)
  })
})
<input type="file" />