我有这份表格提交:
<form action='upload.php' method='post' enctype='multipart/form-data' target='upload_target' onsubmit='startUpload();' >
我试图在上传之前获取文件大小,如果文件太大则停止提交。
iSize
报告的文件大小正确,警报正在显示,但提交仍在继续。
如果显示警报,如何退出上传?
function startUpload(){
var iSize = ($("#myfile")[0].files[0].size / 1024);
console.log(iSize)
if (iSize > 10) {
alert('file size to big');
return;
}
document.getElementById('f1_upload_process').style.visibility = 'visible';
document.getElementById('f1_upload_form').style.visibility = 'hidden';
return true;
}
function stopUpload(success){
var result = '';
if (success == 1){
result = '<span class="msg">The file was uploaded successfully!<\/span><br/><br/>';
}
else {
result = '<span class="emsg">There was an error during file upload!<\/span><br/><br/>';
}
document.getElementById('f1_upload_process').style.visibility = 'hidden';
document.getElementById('f1_upload_form').innerHTML = result + '<label>File: <input name="myfile" type="file" size="30" /><\/label><label><input type="submit" name="submitBtn" class="sbtn" value="Upload" /><\/label>';
document.getElementById('f1_upload_form').style.visibility = 'visible';
return true;
}
答案 0 :(得分:1)
你必须传递函数的返回值:
<form action='upload.php' method='post' enctype='multipart/form-data' target='upload_target' onsubmit='return startUpload();' >
此外,在旁注中,如果您考虑在进程中发生form.submit(),则函数stopUpload()的成功部分没有多大意义。人们必须通过将其发送到iframe或使用ajax来正确使用它来上传它。