从Google Apps脚本将mp3文件上传到Google云端硬盘 [英] Uploading mp3 file to Google Drive from Google Apps Script
问题描述
我有一个Google Apps脚本网络应用程序,可用于将表单响应保存在电子表格和Google驱动器中.
I have a Google Apps Script web app which I use to save form responses in a spreadsheet and Google drive.
其中一个表单字段将文件保存在Google云端硬盘中,但无法正常工作.
One of the form fields saves a file in Google Drive, but it's not working.
HTML表单(由于表单大小,我已将其缩小)
HTML form (I have reduced it due to the form size)
<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,但是当我尝试创建
The data is being sended properly to code.gs, in the server function I receive the file.bytes and the file.mimeType but when I try to create the newBlob it doesn't create anything, returns as undefined. Any thoughts?
推荐答案
我已经解决了该问题.我试图将newBlob存储在变量"fichero"中,然后我做了console.log()来查看内容.它返回未定义的值,我真的很坚信它没有用,但它一直都有效.
I have resolved the issue. I was trying to store the newBlob in the variable "fichero", and after that i done a console.log() to view the content. It returns undefined, and i really tought it wasn't working, but it worked all the time.
我有条件检查文件是否存在,因为客户端表单并不总是有文件.在这里,我将文件上传到Google云端硬盘:
I had a conditional to check if the file existed, because the client form didn't always have a file. Here I upload the file to Google Drive:
if (fichero){
......
carpetaII.createFile(fichero); //Store in Google Drive
}
"fichero"变量返回未定义,所以我从来没有选择将文件存储在Google云端硬盘中.
The "fichero" variable was returning undefined, so i never had the option to store the file in Google Drive.
所以我将if (fichero){}
更改为if (file.bytes){}
,现在它可以正常工作了.
So I changed if (fichero){}
to if (file.bytes){}
and now it works properly.
这篇关于从Google Apps脚本将mp3文件上传到Google云端硬盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!