Google apps script 如何将文件上载blob从HTML表单传递到服务器端应用程序脚本?
表单标题下的Google示例被破坏了。从文章中: 如果使用表单元素作为参数调用服务器函数,则表单将成为单个对象,字段名作为键,字段值作为值。这些值都转换为字符串,但文件输入字段的内容除外,这些字段将成为Blob对象 我通过传递一个包含5个文本输入和一个文件的表单元素,然后在表单对象上记录Google apps script 如何将文件上载blob从HTML表单传递到服务器端应用程序脚本?,google-apps-script,Google Apps Script,表单标题下的Google示例被破坏了。从文章中: 如果使用表单元素作为参数调用服务器函数,则表单将成为单个对象,字段名作为键,字段值作为值。这些值都转换为字符串,但文件输入字段的内容除外,这些字段将成为Blob对象 我通过传递一个包含5个文本输入和一个文件的表单元素,然后在表单对象上记录Object.keys()。它只返回5个文本字段,文件从表单对象中剥离。试图分配直接返回的文件blob异常:无效参数:blob 如何将文件blob从客户端表单传递到服务器端应用程序脚本 编辑:为了澄清,我还复制粘
Object.keys()。它只返回5个文本字段,文件从表单对象中剥离。试图分配直接返回的文件blob异常:无效参数:blob
如何将文件blob从客户端表单传递到服务器端应用程序脚本
编辑:为了澄清,我还复制粘贴了Google逐字提供的示例。It错误与异常:无效参数:blob
复制:
创建新的谷歌应用程序脚本项目
Index.html内容:
//阻止表单提交。
函数preventFormSubmit(){
var forms=document.queryselectoral('form');
对于(var i=0;i
代码G.gs内容:
发布为Web应用程序
随任何文件一起提交表格
观察视图->堆栈驱动程序日志->应用程序脚本仪表板中的错误
下面是一个例子:
html:
下面是一个例子:
html:
我可以确认这在G-Suite企业中不起作用。我不知道为什么,因为我找不到说明谷歌如何序列化数据的文档。它可能是浏览器/计算机安全设置或G-Suite中的某个内容
然而,有一种更简单的方法来满足您的需求。您可以使用带有文件上载问题的Google表单,然后在表单提交上创建一个触发器/事件,将文件复制到团队/共享驱动器。如果要将触发器附加到Google表单本身,下面是示例代码:
// ID of the destnation folder to save the file in
var destinationFolderID = "10gkU_2V9iYy-VKudOCOjydEpoepPTgPv"
function saveFileToTeamDrive(e)
{
// a place to save the URL of the uploaded file
var fileID;
// go through all of the responses to find the URL of the uploaded file
e.response.getItemResponses().forEach(function(itemResponse){
// once we find the question with the file
if(itemResponse.getItem().getTitle() == "File Upload Test")
{
// get the file ID from the response
fileID = itemResponse.getResponse();
return;
}
});
// stop if we didn't have one
if(!fileID.length) return;
// get the first index in the array
fileID = fileID[0];
// get the file
var file = DriveApp.getFileById(fileID);
// get the destination folder
var destinationFolder = DriveApp.getFolderById(destinationFolderID);
// make a copy
var newFile = file.makeCopy(destinationFolder);
Logger.log(newFile.getUrl());
}
您还可以附加到链接到谷歌表单的谷歌表单的on form submit
事件。我发现这样做更容易,因为表单submit
trigger/event上的Google表单包含一个问题/答案的JSON,所以您不必迭代所有问题/答案来查找它。这也意味着,如果提交失败,您可以重新运行提交
警告
一个重要的注意事项是,如果您执行了上述任一操作,请不要给任何其他人对代码的编辑权限。这是因为一旦你创建并授权了触发器,任何对代码有编辑权限的人都可以使用它来访问你的谷歌硬盘(以及触发器授权的任何其他内容)。有关更多信息,请参阅。我可以确认这在G-Suite Enterprise中不起作用。我不知道为什么,因为我找不到说明谷歌如何序列化数据的文档。它可能是浏览器/计算机安全设置或G-Suite中的某个内容
然而,有一种更简单的方法来满足您的需求。您可以使用带有文件上载问题的Google表单,然后在表单提交上创建一个触发器/事件,将文件复制到团队/共享驱动器。如果要将触发器附加到Google表单本身,下面是示例代码:
// ID of the destnation folder to save the file in
var destinationFolderID = "10gkU_2V9iYy-VKudOCOjydEpoepPTgPv"
function saveFileToTeamDrive(e)
{
// a place to save the URL of the uploaded file
var fileID;
// go through all of the responses to find the URL of the uploaded file
e.response.getItemResponses().forEach(function(itemResponse){
// once we find the question with the file
if(itemResponse.getItem().getTitle() == "File Upload Test")
{
// get the file ID from the response
fileID = itemResponse.getResponse();
return;
}
});
// stop if we didn't have one
if(!fileID.length) return;
// get the first index in the array
fileID = fileID[0];
// get the file
var file = DriveApp.getFileById(fileID);
// get the destination folder
var destinationFolder = DriveApp.getFolderById(destinationFolderID);
// make a copy
var newFile = file.makeCopy(destinationFolder);
Logger.log(newFile.getUrl());
}
您还可以附加到链接到谷歌表单的谷歌表单的on form submit
事件。我发现这样做更容易,因为表单submit
trigger/event上的Google表单包含一个问题/答案的JSON,所以您不必迭代所有问题/答案来查找它。这也意味着,如果提交失败,您可以重新运行提交
警告
一个重要的注意事项是,如果您执行了上述任一操作,请不要给任何其他人对代码的编辑权限。这是因为一旦你创建并授权了触发器,任何对代码有编辑权限的人都可以使用它来访问你的谷歌硬盘(以及触发器授权的任何其他内容)。有关更多信息,请参见。正如文档中所述,您应该发送表单元素
而不是表单对象
我使用了错误的单词…我更新了问题。我传递了HTML表单元素。用代码演示如何传递该元素。提供。这是异常:无效参数:blob。
客户端还是服务器端?您的代码在我上传.pdf文件的测试中运行良好。你上传的文件的mime类型是什么?@AndresDuarte我试过各种(txt、xlsx、png),都不管用。听到同样的代码适用于其他人,我现在相信我的公司存在某种公司限制。(◔_◔)正如文档所述,您应该发送表单元素
而不是表单对象
我使用了错误的单词…我更新了问题。我传递了HTML表单元素。显示您如何通过代码传递元素。提供。这是异常:无效参数:blob。
客户端还是服务器端?您的代码在我的测试上传一个.pdf文件。你上传的文件的mime类型是什么?@AndresDuarte我试过各种(txt、xlsx、png),都不管用。听到完全相同的代码对其他人有效,现在我相信我的公司存在某种公司限制(◔_◔)我更新了你的getFolderById方法来检索存在于我的驱动器中的东西,但它仍然不适用于我
function uploadTheFile(theForm) {
var fileBlob=theForm.fileToLoad;
var fldr = DriveApp.getFolderById('FolderId');
var file=fldr.createFile(fileBlob);
var fi=formatFileName(file);
var fileInfo={'name':fi.getName(),'type':fileBlob.getContentType(), 'size':fileBlob.getBytes(), 'folder':fldr.getName()};
return fileInfo;
}
// ID of the destnation folder to save the file in
var destinationFolderID = "10gkU_2V9iYy-VKudOCOjydEpoepPTgPv"
function saveFileToTeamDrive(e)
{
// a place to save the URL of the uploaded file
var fileID;
// go through all of the responses to find the URL of the uploaded file
e.response.getItemResponses().forEach(function(itemResponse){
// once we find the question with the file
if(itemResponse.getItem().getTitle() == "File Upload Test")
{
// get the file ID from the response
fileID = itemResponse.getResponse();
return;
}
});
// stop if we didn't have one
if(!fileID.length) return;
// get the first index in the array
fileID = fileID[0];
// get the file
var file = DriveApp.getFileById(fileID);
// get the destination folder
var destinationFolder = DriveApp.getFolderById(destinationFolderID);
// make a copy
var newFile = file.makeCopy(destinationFolder);
Logger.log(newFile.getUrl());
}