所以基本上任务非常简单,但我没有找到任何可行的解决方案来解决我的问题。我的网站上有一个巨大的上传脚本(目前是本地主机),但让我们将所有复杂性减少到唯一必要的程度。
所以我只想使用 Google App Script 将单个文件上传到 Google 云端硬盘,并接收它的 URL 以将其保存在 var 中,以便稍后在我的函数中使用该信息。
现在的问题是我的网站上已经有该表单,我不希望 script.google .com 内的表单作为额外的 html,我想将我的用户输入传输到 Google App Script,然后将其上传到 google 驱动器并将 url 返回到我的网站,我可以将其保存到 var 中。
我现在的问题是,我无法将所有的东西放在一起。
这是我网站上的表格(简化):
那么我如何在 Google Drive 中上传我的信息并获取结果呢?如何在不使用 iFrame 或其他任何东西的情况下推送 Google App 脚本中的数据?
谢谢!
**** 如果 html 位于 script.google .com 中,则工作示例 ****
GS
function doGet(e) {
return HtmlService.createHtmlOutputFromFile('forms.html').setTitle("Google File Upload by CTRLQ.org");
}
function uploadFileToGoogleDrive(data, file, name, email) {
try {
var dropbox = "Received Files";
var folder, folders = DriveApp.getFoldersByName(dropbox);
if (folders.hasNext()) {
folder = folders.next();
} else {
folder = DriveApp.createFolder(dropbox);
}
/* Credit: www.labnol.org/awesome */
var contentType = data.substring(5,data.indexOf(';')),
bytes = Utilities.base64Decode(data.substr(data.indexOf('base64,')+7)),
blob = Utilities.newBlob(bytes, contentType, file),
file = folder.createFolder([name, email].join(" ")).createFile(blob);
return "OK";
} catch (f) {
return f.toString();
}
}
apps.googlescript 中的 html
Google File Upload by CTRLQ.org
按照建议,我将在这里描述该过程。
所以我们在网站上:www.example.com,有一个带有文本输入字段和文件字段的表单。假设我们放入一张图像并将其称为示例。现在,如果我们按提交,我想将图像上传到谷歌驱动器,而不需要任何oAuth(这就是为什么我们需要在此处使用谷歌应用程序脚本)并将其命名为我们在文本字段中键入的内容。上传完成后,我希望将 google Drive 图像的 url 返回到网站,以便表单可以继续使用该信息。我想将返回的 url 保存在 var 中,稍后将其保存在数据库中。这就是为什么我需要将结果返回到我的网站。
因此方案如下所示:
在网站上输入信息到表单 -> 重定向到 google 应用程序脚本:获取网站表单字段的信息并将文件上传到 google 驱动器,并将其命名为文本输入条目 -> 将 google 驱动器的 url 作为最终结果 -> 重定向最终url 结果返回网站 -> 将 url 结果保存在 var 中并继续从网站 -> 上的函数执行操作,最后将 var 中的信息保存到数据库 -> finish
------------------------------------------------------------ - 编辑: ------------------
感谢@Tanaike,我离我的挑战目标更近了,所以为了看看我陷入了困境,我现在正在复制我的问题:
我使用您示例中的脚本获取了表单:
对于谷歌脚本:
function doPost(e) {
// const folderId = "###"; // Folder ID which is used for putting the file, if you need.
const blob = Utilities.newBlob(JSON.parse(e.postData.contents), e.parameter.mimeType, e.parameter.filename);
const file = DriveApp.getFolderById(folderId || "root").createFile(blob);
const responseObj = {filename: file.getName(), fileId: file.getId(), fileUrl: file.getUrl()};
return ContentService.createTextOutput(JSON.stringify(responseObj)).setMimeType(ContentService.MimeType.JSON);
}
现在,当我尝试上传某些内容时,出现以下错误:CORS 策略无法获取。所以我将这部分更改为以下内容并添加了 no cors 模式:
const qs = new URLSearchParams({filename: form.filename.value || file.name, mimeType: file.type});
fetch(`${url}?${qs}`, {method: "POST", mode: "no-cors", body: JSON.stringify([...new Int8Array(f.target.result)])})
这有效。第二次尝试上传文件导致以下错误:
它说:syntax错误:输入意外结束
所以我更改了这一行并从 res.json 中删除了括号
JSON.stringify([...new Int8Array(f.target.result)])})
.then(res => res.json)
第三次尝试上传实际有效的文件,控制台结果如下:
ƒ json() { [native code] }
但是 Google 云端硬盘中没有上传文件。我在某个地方缺少一些东西。也许我们应该创建一个文件夹并将文件放入其中。
哦,还有另一个信息:当我在 google app sript 中运行 doPost 函数时,它说:
TypeError: Cannot read property 'postData' of undefined (line 13
编辑2 -----------------------------------------------------
我将 https://drive.google .com/uc?export=download&id=###fileId### 添加到您的代码中,一切正常。文件正在上传。
假设我们上传文件 test.mp3,并将其命名为 testdata。 这是我们收到的:
{
"filename": "testdata",
"fileId": "###some id##",
"fileUrl": "https://drive.google .com/uc?export=download&id=###fileId###"
}
现在,当我打开文件 url 时,浏览器会下载文件,但其名称为:testdata,而不是 testdata.mp3。文件类型结尾丢失。
第二个任务:如果您单击链接,我想在浏览器中打开该文件,例如,当它是 mp3 文件时,我希望您可以在 webview 中播放声音,就像这里一样:https://files.freemusicarchive .org/storage-freemusicarchive-org/music/Creative_Commons/Dead_Combo/CC_Affiliates_Mixtape_1/Dead_Combo_-_01_-_Povo_Que_Cas_Decalo.mp3
希望您能指导我!
Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号