From your question, if you are using my sample script of my answer, how about the following modification?
From:
fetch(`${url}?${qs}`, {method: "POST", body: JSON.stringify([...new Int8Array(f.target.result)])})
.then(res => res.json())
.then(e => console.log(e)) // <--- You can retrieve the returned value here.
.catch(err => console.log(err));
To:
fetch(`${url}?${qs}`, {method: "POST", body: JSON.stringify([...new Int8Array(f.target.result)])})
.then(res => res.json())
.then(e => {
window.open('https://###', '_self');
})
.catch(err => console.log(err));
- Please set your redirect URL to
https://###
of window.open('https://###', '_self');
.
Added:
From your following reply,
In Google apps script I've a 2 files one is code.gs and success.html. Now I want return user to success.html (which is in apps script). So, in this scenario how can I achieve this?
I've having hosted the html form. But you can just save it in html format and open locally.
When your provided script is used, please modify as follows.
Google Apps Script side:
Please add the following function to Google Apps Script. In this case, doPost
is not modified. From your reply, it supposes that your HTML filename in your Google Apps Script project is success.html
. Please confirm the filename again. When it is different, please modify the below script.
function doGet() {
return HtmlService.createHtmlOutputFromFile("success.html").setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL);
}
HTML&Javascript side:
Please modify your HTML and Javascript as follows.
<!DOCTYPE html>
<html>
<head>
<title>File Upload to Google Drive with Name and Email Input</title>
</head>
<body>
<form id="form">
<label for="name">Name:</label>
<input name="name" id="name" type="text"><br><br>
<label for="email">Email:</label>
<input name="email" id="email" type="email"><br><br>
<label for="uploadfile">File:</label>
<input name="file" id="uploadfile" type="file"><br><br>
<label for="filename">File Name:</label>
<input name="filename" id="filename" type="text"><br><br>
<input id="submit" type="submit">
</form>
<script>
const form = document.getElementById('form');
form.addEventListener('submit', e => {
e.preventDefault();
const name = form.name.value;
const email = form.email.value;
const file = form.file.files[0];
if (!file) {
const url = "https://script.google.com/macros/s/###/exec"; // Please replace this with your Web Apps URL.
const qs = new URLSearchParams({filename: "", mimeType: "", name: name || "", email: email || ""});
fetch(`${url}?${qs}`, {method: "POST", body: JSON.stringify([])})
.then(res => res.json())
.then(e => {
window.open(url, '_self');
})
.catch(err => console.log(err));
return;
}
const fr = new FileReader();
fr.readAsArrayBuffer(file);
fr.onload = f => {
const url = "https://script.google.com/macros/s/###/exec"; // Please replace this with your Web Apps URL.
const qs = new URLSearchParams({filename: form.filename.value || file.name, mimeType: file.type, name: name || "", email: email || ""});
fetch(`${url}?${qs}`, {method: "POST", body: JSON.stringify([...new Int8Array(f.target.result)])})
.then(res => res.json())
.then(e => {
window.open(url, '_self');
})
.catch(err => console.log(err));
}
});
</script>
</body>
</html>
- In this case, when you open HTML form and click the button, after the file is uploaded,
success.html
is opened by doGet
.
Note: