我有一个Google Apps脚本网络应用程序,可用于将表单响应保存在电子表格和Google驱动器中。
其中一个表单字段将文件保存在Google云端硬盘中,但无法正常工作。
HTML表单(由于表单大小,我已将其缩小)
<form id="miformulario" onsubmit="envio_formulario(this)">
<input type="text" id="padre" name="padre" maxlength=8 value="" required class="form-control"/>
<input type="email" id="mailp" name="mailp" value="" required class="form-control" />
<input type="file" name="documentacion" >
<button type="submit" class="btn btn-outline-secondary">Tramitar solicitud</button>
</form>
form.js
function envio_formulario(Objetoformulario)
{
var values = $('#miformulario').serializeArray();
var data = {};
$(values ).each(function(index, obj){
data[obj.name] = obj.value;
});
var invalid = Objetoformulario.querySelectorAll(':invalid');
if ( invalid.length == 0 ) // Si no hay errores grabamos los datos
{
const file = Objetoformulario.documentacion.files[0];
const fr = new FileReader();
fr.onload = function(e) {
const obj = {
mimeType: file.type,
bytes: [...new Int8Array(e.target.result)]
};
google.script.run.withSuccessHandler(ficherocargado).cargarFichero(obj, data);
};
fr.readAsArrayBuffer(file);
}
}
Code.gs
function cargarFichero(file, form){
console.log(file.mimeType); //Output => audio/mpeg
console.log(typeof file.bytes); //Output => Object
console.log(file.bytes); //Output => [ 82, 73, 70, 70, -128.......]
var fichero = Utilities.newBlob(file.bytes, file.mimeType, "file");
console.log(fichero) // Output => undefined
if (fichero){
var documentosI = "xxxxxx";
var documentosII = cif + "_" + cliente;
var carpetaI, carpetasI = DriveApp.getFoldersByName(documentosI);
var carpetaII, carpetasII = DriveApp.getFoldersByName(documentosII);
if (carpetasI.hasNext()) {
carpetaI = carpetasI.next();
}
if (carpetasII.hasNext()) {
carpetaII = carpetasII.next();
}
else {
carpetaII = carpetaI.createFolder(documentosII);
}
var documentacion = carpetaII.createFile(fichero);
documentacion.setName(cif + " _ " + cliente + " _ ATEN");
var id_documento = documentacion.getId();
}
}
数据已正确发送到code.gs,在服务器功能中我收到了file.bytes和file.mimeType,但是当我尝试创建newBlob时却什么也没创建,返回为未定义。有什么想法吗?
答案 0 :(得分:0)
我已经解决了问题。我试图将newBlob存储在变量“ fichero”中,然后我做了console.log()来查看内容。它返回未定义的值,我真的很坚信它没有用,但是一直都有效。
我有条件检查文件是否存在,因为客户端表单并不总是有文件。在这里,我将文件上传到Google云端硬盘:
if (fichero){
......
carpetaII.createFile(fichero); //Store in Google Drive
}
“ fichero”变量返回的是不确定的,所以我从来没有选择将文件存储在Google云端硬盘中。
因此,我将if (fichero){}
更改为if (file.bytes){}
,现在它可以正常工作了。