Google apps script 使用Google Apps脚本->上载页面上载后文件损坏
我准备了一个简单的谷歌应用程序脚本与上传形式,让我的朋友没有谷歌帐户上传一些不同类型的文件-图片,docx,png等;相当小的文件-低于10MBGoogle apps script 使用Google Apps脚本->上载页面上载后文件损坏,google-apps-script,file-upload,google-drive-api,Google Apps Script,File Upload,Google Drive Api,我准备了一个简单的谷歌应用程序脚本与上传形式,让我的朋友没有谷歌帐户上传一些不同类型的文件-图片,docx,png等;相当小的文件-低于10MB function doGet(e) { return HtmlService.createHtmlOutputFromFile('form.html'); } function uploadFiles(form) { try { var mainFolder = "Tmp_Uploads"; var fo
function doGet(e) {
return HtmlService.createHtmlOutputFromFile('form.html');
}
function uploadFiles(form) {
try {
var mainFolder = "Tmp_Uploads";
var folder = DriveApp.getFoldersByName(mainFolder).next();
var file = folder.createFile(form.myFile);
return "File uploaded successfully " + file.getUrl();
} catch (error) {
return error.toString();
}
}
机制工作->上传后文件显示在我的Tmp_上传文件夹在谷歌驱动器。
但当我下载到我的电脑->文件是坏的
我用.jpg、.png、.doc对它进行了测试,它们都坏了——无法打开,它们的大小也和原来的不同
当我上传simple.txt文件->它工作正常
撤销:
怎么了?
我是否可以以某种方式恢复使用上述机制上载的文件?在下载到PC或后处理后对其进行解码,以便能够打开它?
干杯并提前表示感谢 在调用google script uploadFiles函数之前,在html中尝试此Javascript函数
<script>
function uploadFile(form) {
const file=form.File.files[0];
const fr=new FileReader();//all this FileReader code originally came from Tanaike
fr.onload=function(e) {
const obj={filename:file.name,mimeType:file.type,bytes:[...new Int8Array(e.target.result)]};
google.script.run.uploadFiles(obj);
}
fr.readAsArrayBuffer(file);
}
</script>
如果您的表单中有更多的数据,而不仅仅是一个文件上传,那么看看这个例子
<script>
function uploadFile(form) {
console.log('form.elements.Destination.value= %s',form.elements.Destination.value);
console.log('format.elements.FileName.value= %s',form.elements.FileName.value);
$('#msg').css('display','block');
$('#msg').html('UpLoading...Please Wait');
const file=form.File.files[0];
const fr=new FileReader();//all this FileReader code originally came from Tanaike
fr.onload=function(e) {
const obj={FileName:form.elements.FileName.value,Destination:form.elements.Destination.value,mimeType:file.type,bytes:[...new Int8Array(e.target.result)]};//this allowed me to put the rest of the Form.elements back into the object before sending it to google scripts
google.script.run
.withSuccessHandler(function(msg){
$('#msg').css('display','block');
$('#msg').html(msg);//I returned a message after the download
})
.uploadFile(obj);
}
fr.readAsArrayBuffer(file);
}
</script>
<form>
<br /><select id="sel1" name="Destination"></select> Upload Destination
<br /><input type="text" name='FileName' /> File Name
<br /><input type="file" name='File'; />
<br /><input type="button" value="Upload" onClick="uploadFile(this.parentNode);" />
<br /><input type="button" value="Close" onclick="google.script.host.close();" />
<div id="msg"></div>
</form>
是否正在运行V8?这是否回答了您的问题?在提问之前请先回答。同意-正在尝试搜索,但不知何故没有找到您提到的内容。谢谢不知道这是V8的问题,甚至不知道我用的是V8的应用程序脚本。你对问题2有什么建议吗?谢谢-就是这样-我用你的建议调整了我的代码-现在没问题了。但第二个问题呢?你们认为有可能用我在第一篇文章中描述的机制对上传的文件进行后处理吗?我能不能破译一下,以便能正确阅读呢?我建议你作为另一个单独的问题再问一遍。在我的情况下,我很高兴了解到我以前从未使用过的文件阅读器,我假设在使用它之前我试图上传的文件实际上没有被上传,如果你说它们正在被上传,那么我不知道你问题的答案,所以我会通过问另一个问题将其重定向到另一个人。
<script>
function uploadFile(form) {
console.log('form.elements.Destination.value= %s',form.elements.Destination.value);
console.log('format.elements.FileName.value= %s',form.elements.FileName.value);
$('#msg').css('display','block');
$('#msg').html('UpLoading...Please Wait');
const file=form.File.files[0];
const fr=new FileReader();//all this FileReader code originally came from Tanaike
fr.onload=function(e) {
const obj={FileName:form.elements.FileName.value,Destination:form.elements.Destination.value,mimeType:file.type,bytes:[...new Int8Array(e.target.result)]};//this allowed me to put the rest of the Form.elements back into the object before sending it to google scripts
google.script.run
.withSuccessHandler(function(msg){
$('#msg').css('display','block');
$('#msg').html(msg);//I returned a message after the download
})
.uploadFile(obj);
}
fr.readAsArrayBuffer(file);
}
</script>
<form>
<br /><select id="sel1" name="Destination"></select> Upload Destination
<br /><input type="text" name='FileName' /> File Name
<br /><input type="file" name='File'; />
<br /><input type="button" value="Upload" onClick="uploadFile(this.parentNode);" />
<br /><input type="button" value="Close" onclick="google.script.host.close();" />
<div id="msg"></div>
</form>