我有一个直到昨天都运转良好的项目。 我的Chrome已更新为最新版本。问题是我无法下载和上传文件。 但是,当我将Chrome版本降级时,它似乎可以正常工作。 我可以知道这种现象的原因。
先谢谢了。
以下是我的代码:
<form action="" method="post" name="worklist_upload_form" onsubmit="return worklist_upload(this);" enctype="multipart/form-data">
<input type="file" name="upload_worklist" id="upload_worklist" style="width:200px;" accept=".csv" />
</form>
以下是我的js代码:
function worklist_upload(){
if(document.getElementById("upload_worklist").value == ""){
//document.getElementById("error_upload_worklist").style.display = "block";
dispmsg("error","Warning","Please upload the worklist");
// document.getElementById("error_upload_worklist").innerHTML = "<img src='images/red_wrong.png' width='12' alt='wrong' /> Please upload the worklist";
document.getElementById("upload_worklist").focus();
return false;
}
if(document.getElementById("upload_worklist").value != ""){
var fileName = document.getElementById("upload_worklist").value;
var ext = fileName.substring(fileName.lastIndexOf('.') + 1);
if( ext == "xls" || ext == "xlsx") {
//return true;
}else{
dispmsg("error","Warning","Please select .xls or .xlsx format");
/* document.getElementById("error_upload_worklist").innerHTML = "<img src='images/red_wrong.png' width='12' alt='wrong' /> Please select .csv or xls format";
document.getElementById("upload_worklist").focus();*/
return false;
}
}
}
答案 0 :(得分:0)
在您的JavaScript代码上,最好检查ext
不是否等于.xls或.xlsx并显示错误(如果不相等)。
因此您的JavaScript代码应如下:
function worklist_upload(){
if(document.getElementById("upload_worklist").value == ""){
//document.getElementById("error_upload_worklist").style.display = "block";
dispmsg("error","Warning","Please upload the worklist");
// document.getElementById("error_upload_worklist").innerHTML = "<img src='images/red_wrong.png' width='12' alt='wrong' /> Please upload the worklist";
document.getElementById("upload_worklist").focus();
return false;
}
if(document.getElementById("upload_worklist").value != ""){
var fileName = document.getElementById("upload_worklist").value;
var ext = fileName.substring(fileName.lastIndexOf('.') + 1);
if( ext !== "xls" && ext !== "xlsx") {
dispmsg("error","Warning","Please select .xls or .xlsx format");
/* document.getElementById("error_upload_worklist").innerHTML = "<img src='images/red_wrong.png' width='12' alt='wrong' /> Please select .csv or xls format";
document.getElementById("upload_worklist").focus();*/
return false;
}else{
return true;
}
}
}
此外,您的HTML表单接受.csv
,而JavaScript代码检查.xls
或.xlsx
编辑:您还可以通过其他方式(不是.xls
来检查扩展名不是.xlsx
还是if( ext !== "xls" && ext !== "xlsx")
,
if(["xls","xlsx"].indexOf(ext) == -1) {
// Display error messages
return false;
}