如何通过 POST (doPost) 将文件上传到 Google Script 的 Web 应用程序? [英] How to upload a file via POST (doPost) to a Google Script's Web App?

查看:25
本文介绍了如何通过 POST (doPost) 将文件上传到 Google Script 的 Web 应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题和这个完全一样:使用 doPost 将文件上传到 Google Web Apps

My question is exactly as this one: Upload a file to a Google Web Apps with doPost

如何将文件从另一个域直接上传到 Google Script 的 Web 应用程序?我在那个问题中回答了一个解决方法,将文件转换为 base64 字符串,但问题仍然存在.是否可以将文件上传到 Google Scripts 或仅上传字符串?

How to upload a file directly to a Google Script's Web App from another domain? I answered a workaround in that question to convert the file in a base64 string but the question remains. Is it possible to upload a file to Google Scripts or only strings?

推荐答案

文件无法通过 GAS 的doPost()"从本地 PC 上的 HTML 表单中直接上传.因为multipart/form-data"可能无法用于doPost()".因此,如您所说,转换 Base64 会导致解决方案.

Files cannot be uploaded directly by "doPost()" of GAS from HTML form on local PC. Because "multipart/form-data" may be not able to be used for "doPost()". So converting Base64 leads to the solution as you say.

通过doPost()"上传文件有两种方式.

There are 2 ways for uploading files by "doPost()".

1.从 GAS 项目的 HTML 表单上传文件

这会在 GAS 项目中使用 GAS 和 HTML 上传文件.在这种情况下,文件不会通过脚本转换为 Base64.(它可能会转换为内部过程.)

This uploads a file using GAS and HTML in GAS project. In this case, the file doesn't convert to Base64 by the script. (It may convert as the internal process.)

2.从本地 PC 上的 HTML 表单上传文件

这会从本地 PC 上的 HTML 表单上传文件.GAS 控制doPost()".在这种情况下,文件被编码为Base64并作为文本数据上传,然后使用GAS解码为文件.

This uploads a file from HTML form on local PC. GAS controls "doPost()". In this case, the file is encoded to Base64 and upload as text data, and then decoded to the file using GAS.

规则:

  1. 以下示例脚本和 HTML 必须制作成 Google Apps 脚本的项目.

  1. Following sample script and HTML have to be made into a project of Google Apps Script.

将 GAS 项目部署为 Web 应用程序.https://developers.google.com/apps-script/guides/web并检索网址.

Deploy the GAS project as a web application. https://developers.google.com/apps-script/guides/web And retrieve URL.

更新脚本后,必须将其更新为新版本.

After updated the script, it has to be updated as a new version.

Form.html :

<html>
  <body>
    <form>
      <input type="file" name="upFile">
      <input type="button" value="ok" onclick="google.script.run.upload(this.parentNode)">
    </form>
  </body>
</html>

脚本:

function doGet() {
  return HtmlService.createHtmlOutputFromFile('Form.html');
}

function upload(e) {
  DriveApp.createFile(e.upFile);
}

<小时>

2.从本地 PC 上的 HTML 表单上传文件

规则与上面的几乎相同.该脚本放置在 GAS 项目中.但是HTML表单放在本地PC上.


2. Upload a file from HTML form on local PC

The rule is almost the same to above one. The script is placed on GAS project. But HTML form is placed on local PC.

脚本:

function doGet(e) {
  return message("Error: no parameters");
}

function doPost(e) {
  if (!e.parameters.filename || !e.parameters.file) {
    return message("Error: Bad parameters");
  } else {
    var data = Utilities.base64Decode(e.parameters.file, Utilities.Charset.UTF_8);
    var blob = Utilities.newBlob(data, MimeType.PNG, e.parameters.filename);
    DriveApp.createFile(blob);
    return message("completed");
  }
}

function message(msg) {
  return ContentService.createTextOutput(JSON.stringify({result: msg})).setMimeType(ContentService.MimeType.JSON);
}

HTML :

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <script src="http://code.jquery.com/jquery-latest.js" type="text/javascript"></script>
</head>
<body>
    <input type="file" id="file">

    <script type="text/javascript">
        $(function(){
            var url = 'https://script.google.com/macros/s/[Project ID]/exec';
            var params = {
                filename: 'samplefile',
                imageformat: 'PNG'
            };

            $('#file').on("change", function() {
                var file = this.files[0];
                var fr = new FileReader();
                fr.onload = function(e) {
                    params.file = e.target.result.replace(/^.*,/, '');
                    postJump();
                }
                fr.readAsDataURL(file);
            });

            function postJump(){
                var html = '<form method="post" action="'+url+'" id="postjump" style="display: none;">';
                Object.keys(params).forEach(function (key) {
                    html += '<input type="hidden" name="'+key+'" value="'+params[key]+'" >';
                });
                html += '</form>';
                $("body").append(html);
                $('#postjump').submit();
                $('#postjump').remove();
            }
        });
    </script>
</body>
</html>

这个示例脚本假设上传一个 PNG 文件作为测试.如果您想上传其他文件,请更改 MIME 类型.https://developers.google.com/apps-script/reference/base/mime 类型

This sample script supposes to upload a PNG file as a test. If you want to upload other files, please change mime type. https://developers.google.com/apps-script/reference/base/mime-type

对于 HTML 示例,选择文件时上传文件.

For HTML sample, the file is uploaded when the file is selected.

如果这对您有帮助,我很高兴.

If this will be helpful for you, I'm glad.

这篇关于如何通过 POST (doPost) 将文件上传到 Google Script 的 Web 应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆