这是我的脚本。我想要做的是使用onchange事件触发它。 但似乎效果不好。我曾尝试过编辑,但仍面临问题。至于我下面的最新编辑,问题是,有2个文件要上传,但只有第二个文件会显示警告信息,而第一个文件只会收到任何文件。
<script>
function checkFile()
{
var node_list = document.getElementsByTagName('input');
for (var i = 0; i < node_list.length; i++)
{
var node = node_list[i];
if (node.getAttribute('type') == 'file')
{
var sFileName = node.value;
var sFileExtension = sFileName.split('.')[sFileName.split('.').length - 1];
var iFileSize = node.files[0].size;
var iConvert=(node.files[0].size/10485760).toFixed(2);
}
if (sFileExtension != "pdf" && sFileExtension != "doc" && sFileExtension != "docx" && iFileSize>10485760)
{
txt="File type : "+ sFileExtension+"\n\n";
txt+="Size: " + iConvert + " MB \n\n";
txt+="Please make sure your file is in pdf or doc format and less than 10 MB.\n\n";
alert(txt);
}
}
}
</script>
我的表格,
<input type="file" name="file" id="confirm" onchange="checkFile()">
<input type="file" name="file" id="approveletter" onchange="checkFile()">
答案 0 :(得分:19)
不是依赖于它们自身的元素,而是应该使用给定的事件来获取触发调用回调函数的文件。
传入事件将保证您实际上正在处理导致函数被调用的当前文件。
例如(我更改了变量名称以使其更清晰):
<强> ONLINE DEMO HERE
/// pass in event
function checkFile(e) {
/// get list of files
var file_list = e.target.files;
/// go through the list of files
for (var i = 0, file; file = file_list[i]; i++) {
var sFileName = file.name;
var sFileExtension = sFileName.split('.')[sFileName.split('.').length - 1].toLowerCase();
var iFileSize = file.size;
var iConvert = (file.size / 1048576).toFixed(2);
/// OR together the accepted extensions and NOT it. Then OR the size cond.
/// It's easier to see this way, but just a suggestion - no requirement.
if (!(sFileExtension === "pdf" ||
sFileExtension === "doc" ||
sFileExtension === "docx") || iFileSize > 10485760) { /// 10 mb
txt = "File type : " + sFileExtension + "\n\n";
txt += "Size: " + iConvert + " MB \n\n";
txt += "Please make sure your file is in pdf or doc format and less than 10 MB.\n\n";
alert(txt);
}
}
}
答案 1 :(得分:1)
这可能会有所帮助
var file = document.getElementById("filex").files[0];
var filename = file.name;
var extSplit = filename.split('.');
var extReverse = extSplit.reverse();
var ext = extReverse[0];
if(!ext === "mp4" || !ext === "flv"){
alert('Accepted');
} else {
alert('Not accepted');
}
答案 2 :(得分:1)
您可以在html中将此代码与文件控制器一起使用。任何只传递参数并查看输出
<script type="text/javascript">
function AlertFilesize(cid,sz,a){
var controllerID = cid;
var fileSize = sz;
var extation = a;
if(window.ActiveXObject){
var fso = new ActiveXObject("Scripting.FileSystemObject");
var filepath = document.getElementById('fileInput').value;
var thefile = fso.getFile(filepath);
var sizeinbytes = thefile.size;
}else{
var sizeinbytes = document.getElementById('fileInput').files[0].size;
}
var fSExt = new Array('Bytes', 'KB', 'MB', 'GB');
fSize = sizeinbytes; i=0;while(fSize>900){fSize/=1024;i++;}
var fup = document.getElementById('fileInput');
var fileName = fup.value;
var ext = fileName.substring(fileName.lastIndexOf('.') + 1);
if(ext == "gif" || ext == "GIF" || ext == "JPEG" || ext == "jpeg" || ext == "jpg" || ext == "JPG")
{
var fs = Math.round(fSize);
if(fs < fileSize && fSExt[i] == extation)
{
alert("Image Successfully Uploaded");
return true;}else{
alert("Please enter the image size less then"+fileSize+extation);
document.getElementById('fileInput').value='';
return false;
}
}else{
alert("Must be jpg and gif images ONLY");
document.getElementById('fileInput').value='';
return false;}
}
</script>
<input id="fileInput" type="file" onchange="AlertFilesize(this.id,100,'KB');" />
答案 3 :(得分:1)
<script type="text/javascript">
function setFileSize() {
var fileEl = document.getElementById('<%=FileUpload1.ClientID%>');
var fileSize = fileEl.files[0].size / 1024 / 1024;
var fileName = fileEl.files[0].name;
var dotPosition = fileName.lastIndexOf(".");
var fileExt = fileName.substring(dotPosition);
if (fileSize > 1) {
fileEl.value = '';
document.getElementById('<%=lblFileStatus.ClientID%>').innerHTML = 'File Should Be less Than 1MB';
return false;
}
}
答案 4 :(得分:0)
看看这个小提琴。这是一个很好的工作。正如我所说,你的if条件有所改变。
if (sFileExtension != "pdf" && sFileExtension != "doc" && sFileExtension != "docx" && iFileSize>10485760)
将其替换为
if ((sFileExtension != "pdf" && sFileExtension != "doc" && sFileExtension != "docx") || iFileSize>10485760))
答案 5 :(得分:0)
Class xxx '(this was your 'myclass')
Private _p1 As String
Private _p2 As String
Private _p3 as String
Sub New(p1 As String, p2 As String)
' TODO: Complete member initialization
_p1 = p1
_p2 = p2
End Sub
Sub New(p1 As String, p2 As String, p3 as String)
' TODO: Complete member initialization
_p1 = p1
_p2 = p2
_p3 = p3
End Sub
End Class
答案 6 :(得分:0)
<input type="file" onchange="checkFile(this)" class="form-control" name="doc_file[]">
<script>
function checkFile(item){
var extension = $(item).val().split('.').pop().toLowerCase();
// Create array with the files extensions that we wish to upload
var validExtensions = ['jpeg', 'jpg', 'cdr', 'pdf', 'tiff'];
//Check file extension in the array.if -1 that means the file extension is not in the list.
if ($.inArray(extension, validExtensions) == -1) {
$('#errormsg').text("Failed! Please upload jpg, jpeg, cdr, tiff, pdf file only.").show();
// Clear fileuload control selected file
$(item).replaceWith($(item).val('').clone(true));
//Disable Submit Button
$('#submit').prop('disabled', true);
}
else {
// Check and restrict the file size to 32 KB.
if ($(item).get(0).files[0].size > (20971520)) {
$('#errormsg').text("Failed!! Max allowed file size is 20 MB").show();
// Clear fileuload control selected file
$(item).replaceWith($(item).val('').clone(true));
//Disable Submit Button
$('#submit').prop('disabled', true);
}
else {
//Clear and Hide message span
$('#errormsg').text('').hide();
//Enable Submit Button
$('#submit').prop('disabled', false);
}
}
};
</script>
答案 7 :(得分:0)
这是我的解决方案
function checkFile(field){
let imagePath = field.value;
let allowedExtensions = /(\.jpg|\.png|\.jpeg|\.JPG|\.PNG|\.JPEG|\.jPg|\.pNg|\.jPeg|\.jpG|\.pnG|\.jpEg|\.JPg|\.PNg|\.JPeg)$/i;
if (field.files[0].size > (2 * 1048576)){ // 2 MB
// size exceeds
}else if (!allowedExtensions.exec(imagePath)) {
// extensions does not match
} else {
// everything OK
}
}