提交表单后直接转到新页面

时间:2018-09-11 15:28:08

标签: html google-apps-script

当前,我已经开发了一个Google Scripts API,供人们将文件上传到共享的Google云端硬盘文件夹中。成功上传文件后,我希望将他们带到单独的“谢谢”页面,以便清楚他们的上传已成功。目前,我在同一页面上只有一条消息可以达到此目的,并且我无法弄清楚如何定向到我创建的新页面。

这是我从其他问题中发现的其他内容,可尝试直接进入新页面,但是到目前为止,该功能仍然无效,因为它仍保留在同一上传表单页面上。我已将其包含在我的code.gs文件的底部。关于如何定向到仅显示“谢谢”或类似内容的自定义页面的任何想法都会很棒!

function doPost(e) {
  var template = HtmlService.createTemplateFromFile('Thanks.html');
  return template.evaluate();
}

我其余的代码如下:

Code.gs:

 function doGet() {
        return HtmlService.createHtmlOutputFromFile('form').setSandboxMode(
                HtmlService.SandboxMode.IFRAME);
    }

function createFolder(parentFolderId, folderName) {
    try {
        var parentFolder = DriveApp.getFolderById(parentFolderId);
        var folders = parentFolder.getFoldersByName(folderName);
        var folder;
        if (folders.hasNext()) {
            folder = folders.next();
        } else {
            folder = parentFolder.createFolder(folderName);
        }
        return {
            'folderId' : folder.getId()
        }
    } catch (e) {
        return {
            'error' : e.toString()
        }
    }
}

function uploadFile(base64Data, fileName, folderId) {
    try {
        var splitBase = base64Data.split(','), type = splitBase[0].split(';')[0]
                .replace('data:', '');
        var byteCharacters = Utilities.base64Decode(splitBase[1]);
        var ss = Utilities.newBlob(byteCharacters, type);
        ss.setName(fileName);

        var folder = DriveApp.getFolderById(folderId);
        var files = folder.getFilesByName(fileName);
        var file;
        while (files.hasNext()) {
            // delete existing files with the same name.
            file = files.next();
            folder.removeFile(file);
        }
        file = folder.createFile(ss);
        return {
            'folderId' : folderId,
            'fileName' : file.getName()
        };
    } catch (e) {
        return {
            'error' : e.toString()
        };
    }
}

function doPost(e) {
  var template = HtmlService.createTemplateFromFile('Thanks.html');
  return template.evaluate();
}

Form.html:

<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>


<body>

<div align="center">
  <p><img src="https://drive.google.com/uc?export=download&id=0B1jx5BFambfiWDk1N1hoQnR5MGNELWRIM0YwZGVZNzRXcWZR"
  height="140" width="400" ></p>
<div>

    <form id="uploaderForm">
        <label for="uploaderForm">  <b> Welcome to the Tesco's animal welfare and soy reporting system. </b> </label>
<BR>
<BR>


<div style="max-width:500px; word-wrap:break-word;">

Please add your contact information below and attach a copy of your company's animal welfare standard before clicking submit. Wait for the browser to confirm your submission and you may then close this page.

<BR>
<BR>

Thank you very much for your submission.

</div>

<BR>
<BR>
        <div>
            <input type="text" name="applicantName" id="applicantName"
                placeholder="Your Name">
        </div>

<BR>
        <div>
            <input type="text" name="applicantEmail" id="applicantEmail"
                placeholder="Your Company">
        </div>

<BR>
<BR>
        <div>
            <input type="file" name="filesToUpload" id="filesToUpload" multiple>
            <input type="button" value="Submit" onclick="uploadFiles()">
        </div>
    </form>
    <br>
    <br>
    <br>
    <br>
    <br>
    <br>
    <div id="output"></div>
    <script>
        var rootFolderId = '1-aYYuTczQzJpLQM3mEgOkWsibTak7KE_';
        var numUploads = {};
        numUploads.done = 0;
        numUploads.total = 0;
        // Upload the files into a folder in drive
        // This is set to send them all to one folder (specificed in the .gs file)
        function uploadFiles() {
            var allFiles = document.getElementById('filesToUpload').files;
            var applicantName = document.getElementById('applicantName').value;
            if (!applicantName) {
                window.alert('Missing applicant name!');
            }
            var applicantEmail = document.getElementById('applicantEmail').value;
            if (!applicantEmail) {
                window.alert('Missing applicant email!');
            }
            var folderName = applicantEmail;
            if (allFiles.length == 0) {
                window.alert('No file selected!');
            } else {
                numUploads.total = allFiles.length;
                google.script.run.withSuccessHandler(function(r) {
                    // send files after the folder is created...
                    for (var i = 0; i < allFiles.length; i++) {
                        // Send each file at a time
                        uploadFile(allFiles[i], r.folderId);
                    }
                }).createFolder(rootFolderId, folderName);
            }
        }
        function uploadFile(file, folderId) {
            var reader = new FileReader();
            reader.onload = function(e) {
                var content = reader.result;
                document.getElementById('output').innerHTML = 'uploading '
                        + file.name + '...';
                //window.alert('uploading ' + file.name + '...');               
                google.script.run.withSuccessHandler(onFileUploaded)
                        .uploadFile(content, file.name, folderId);
            }
            reader.readAsDataURL(file);
        }
        function onFileUploaded(r) {
            numUploads.done++;
            document.getElementById('output').innerHTML = 'uploaded '
                    + r.fileName + ' (' + numUploads.done + '/'
                    + numUploads.total + ' files).';
            if (numUploads.done == numUploads.total) {
                document.getElementById('output').innerHTML = 'All of the '
                        + numUploads.total + ' files are uploaded';
                numUploads.done = 0;
            }
        }
    </script>

        <label for="uploaderForm"> 

Powered by 3Keel 
</label>
</body>


</html>

Thanks.html:

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>
    Thank you for submitting!
  </body>
</html>

编辑:

我已根据建议更改了此功能:

        if (numUploads.done == numUploads.total) {
            window.location = 'Thanks.html';
            numUploads.done = 0;

现在它重定向到另一个页面,但是我遇到了这个错误:

  1. 那是一个错误。

在此服务器上找不到请求的URL。这就是我们所知道的。

2 个答案:

答案 0 :(得分:2)

如果您正在寻找问题的解决方案,该答案如何?

  • 您要在Thanks.html处的处理完成后打开Form.html
  • Form.htmlThanks.html放在一个项目中。

如果我的理解是正确的,该解决方法如何?我经历过你的情况。那时,我可以通过此替代方法解决此问题。

修改点:

  • 不需要使用doPost()来访问Thanks.html。我认为您可以使用doGet()来实现自己的目标。
  • 我认为@Scott Craig's answer也可以用于这种情况。在我的解决方法中,window.location = 'Thanks.html';的URL被修改了。
    • 使用已部署的Web Apps的URL。在您的脚本中,当用户访问您的表单时,他们将访问已部署的Web Apps的URL。在这种解决方法中,通过添加查询参数来使用它。

修改后的脚本:

Form.html

对于在问题中添加为“ EDIT”的脚本,请进行以下修改。

从:
window.location = 'Thanks.html';
至:
window.location = 'https://script.google.com/macros/s/#####/exec?toThanks=true';

https://script.google.com/macros/s/#####/exec是已部署的Web Apps的URL。请添加一个查询参数,例如toThanks=true。这是一个示例查询参数。

Code.gs

请如下修改doGet()

从:
function doGet() {
  return HtmlService.createHtmlOutputFromFile('form')
  .setSandboxMode(HtmlService.SandboxMode.IFRAME);
}
至:
function doGet(e) {
  if (e.parameter.toThanks) {
    return HtmlService.createHtmlOutputFromFile('Thanks')
    .setSandboxMode(HtmlService.SandboxMode.IFRAME)
    .setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL);
  } else {
    return HtmlService.createHtmlOutputFromFile('form')
    .setSandboxMode(HtmlService.SandboxMode.IFRAME);
  }
}

注意:

  • 修改了已部署的Web Apps的脚本后,请将其重新部署为新版本。这样,最新的脚本就会反映到Web Apps。
  • 此修改后的脚本的流程如下。
    • 当用户访问Form.html时,由于未使用toThanks=true的查询参数,因此返回Form.html
    • 运行onFileUploaded()if (numUploads.done == numUploads.total) {}为true时,它将使用查询参数toThanks=true打开Web Apps URL。这样,if (e.parameter.toThanks) {}中的doGet()为真,并返回Thanks.html并将其打开。

在我的环境中,我可以确认此修改后的脚本有效。但是,如果这在您的环境中不起作用,对不起。当时,我想考虑一下这个问题。

答案 1 :(得分:0)

我可能会误解您的问题,但是根据我的理解,不是这行:

document.getElementById('output').innerHTML = 'All of the '
                    + numUploads.total + ' files are uploaded';

您要重定向到Thanks.html。如果正确,则将上面的行替换为:

window.location = 'Thanks.html';