我一直在搜索这个问题很长一段时间,但没有找到好的答案。
我有四个字段接受来自用户的文件
<form method="post" action="upload" target="_blank" enctype="multipart/form-data" style="position: absolute; right: 0%; top: 2%;">
Left File : <input type="file" name="dataFile" id="fileChooser" /><br><br>
Right File : <input type="file" name="dataFile" id="fileChooser" /><br><br>
Config File :<input type="file" name="dataFile" id="fileChooser" /><br><br>
my File : <input type="file" name="dataFile" id="fileChooser" /><br><br>
upload<input type="submit" value="Upload" multiple="multiple" />
</form>
如何确定用户是否选择上传同一文件四次并阻止它(即重复上传)?
这可能在JSP端或sevlet java端。 使用commons文件上传。 我在我的搜索here上发现我需要使用DigestOutputStream,但是我找不到如何使用它并且没有用处。
---------------------------------------------编辑 - -------------------------------------
根据以下答案,我更新了我的代码,
<form method="post" name="myform" action="upload" target="_blank" enctype="multipart/form-data" style="position: absolute; right: 0%; top: 2%;">
Left File : <input type="file" name="dataFile1" id="fileChooser1" /><br><br>
Right File : <input type="file" name="dataFile2" id="fileChooser2" /><br><br>
Config File :<input type="file" name="dataFile3" id="fileChooser3" /><br><br>
Geco File : <input type="file" name="dataFile4" id="fileChooser4" /><br><br>
</form>
<button type="button" onclick="ValidateFile()" style="position: absolute; right: 8%; top: 20%;">Click to Upload</button>
<script type='text/javascript'>
function ValidateFile()
{
var FileName1 = document.getElementById("fileChooser1");
var FileName2 = document.getElementById("fileChooser2");
var FileName3 = document.getElementById("fileChooser3");
var FileName4 = document.getElementById("fileChooser4");
if(FileName1 == FileName2)
{
alert("Same file cannot be uploaded!");
}
document.myform.submit(); // This works fine, but the alert doesn't. Tried .value and .value() also still doesn't work.
}
</script>
仍然代码不起作用我的错误是什么?
答案 0 :(得分:0)
Left File : <input type="file" name="dataFile1" id="fileChooser1" /><br><br>
Right File : <input type="file" name="dataFile2" id="fileChooser2" /><br><br>
Config File :<input type="file" name="dataFile3" id="fileChooser3" /><br><br>
my File : <input type="file" name="dataFile4" id="fileChooser4" /><br><br>
为每个输入分配唯一ID并单击上传按钮,调用validateFiles()函数。此功能将验证所选文件。
function validateFiles(){
var fileName1=document.getElementById("fileChooser1").value;
var fileName2=document.getElementById("fileChooser2").value;
............
if(fileName1 == fileName2 ...){
return false;
}
//Here goes Code to Submit form
}
答案 1 :(得分:0)
我认为你的意思是重复意味着按内容而不是文件名重复。如果它只是通过文件名,那么它很容易被@Dark Knight建议。
如果您想通过内容测试双重性,请执行以下简要步骤:
1) Write the contents of the files to temperory files / permenant ones if you are anyway
going to store them.
2) While constructing the outputstream for writing the file,instead of creating normal
FileoutourStream create DigestOutputStream and pass the FileOutputStream to
DigestOutputStream's constructor.
3) DigestOutputStream's constructor takes another parameter called MessageDigest.You can
instantiate this seperately :MessageDigest md = MessageDigest.getInstance("MD5");
and pass on this instance to constructor in step 2.
4) after you call write on the output stream and done with the write calls,
Call one of the digest methods on the MessageDigest instance as follows:
mdos.getMessageDigest().digest().
6) store the string representation of byte array returned by digest by using technique
as suggested in following link :
Get MD5 String from Message Digest
希望它有所帮助。如果您需要澄清,请告诉我
答案 2 :(得分:0)
尝试使用value属性,如下所示:
var file1= document.getElementById("FileUpload1");
var file2= document.getElementById("FileUpload2");
if(file1.value == file2.value){
alert("Duplicate ");
}
您应该执行以下操作来覆盖提交按钮的行为:
<form method="post" onsubmit="return doSomething()">
<input type="file" name="file1">
<input type=submit>
</form>
在你做的事情中做了以下事情:
function doSomething(){
// if you dont want to sumbit return false ;
return false;
//if you want to submit return true
return true;
}
并请求给我一些反馈
希望有所帮助。