我创建了一个表单,用于将文件上传到我的Google云端硬盘。
现在我需要验证并提交表单。所以我想验证输入第一个,然后提交,但我不能这样做。当点击“上传”时,两件事情同时发生。有人能帮助我吗?
HTML:
<div>
<form name= "myForm" id="myForm">
<label><b>Name</b></label>
<input id="id1" type="text" name="myName" placeholder="Name.." required />
<label><b>Email</b></label>
<input id="id1" type="email" name="myEmail" placeholder="Email.." required />
<label><b>Tel</b></label>
<input id="id1" type="tel" name="myTel" placeholder="Tel.." required />
<input id="id1" type="file" name="myFile" required />
<button id="button" onclick="myFunction();">Upload</button>
</form>
</div>
<div id="output"></div>
使用Javascript:
<script>
function myFunction() {
var upload = this.value='Uploading..';
google.script.run.withSuccessHandler(fileUploaded).uploadFiles(this.parentNode);
return false;
var inpObj = document.getElementById("id1");
if (inpObj.checkValidity() == false) {
document.getElementById("button").innerHTML = inpObj.validationMessage; return;
} else {
document.getElementById("button").innerHTML = upload;
}
}
function fileUploaded(status) {
document.getElementById('myForm').style.display = 'none';
document.getElementById('output').innerHTML = status;
}
</script>
此外,这是Server.gs:
function doGet(e) {
return HtmlService.createHtmlOutputFromFile('form.html');
}
function uploadFiles(form) {
try {
var dropbox = "Videos";
var folder, folders = DriveApp.getFoldersByName(dropbox);
if (folders.hasNext()) {
folder = folders.next();
} else {
folder = DriveApp.createFolder(dropbox);
}
var blob = form.myFile;
var file = folder.createFile(blob);
file.setDescription("Uploaded by " + "|" + form.myName + "|" + form.myTitle + "|" + form.myEmail);
return "Success.";
} catch (error) {
return error.toString();
}
}