使用Google API时为空白PDF文件



我正在使用谷歌应用程序脚本,使用户可以将PDF文件上传到我的谷歌驱动器文件夹中。乍一看,一切似乎都很完美,但当我去文件夹检查收到的PDF文件时,我只得到空白的PDF!

有人已经遇到这个问题了吗?如果可以,你能帮我吗?

这是我的两个脚本:

服务器.gs:

function doGet(e) {
return HtmlService.createHtmlOutputFromFile('form.html');
}
function uploadFiles(form) {
try {
var dropbox = "Travaux_T°S";
var folder, folders = DriveApp.getFoldersByName(dropbox);
if (folders.hasNext()) {
folder = folders.next();
} else {
folder = DriveApp.createFolder(dropbox);
}
var blob = form.myFile;    
var file = folder.createFile(blob);    
file.setDescription("Uploaded by " + form.myName);
return "Et hop: une copie de plus à corriger ! :)" ;
} catch (error) {
return error.toString();
}
}

form.html:

<!doctype html>
<style type="text/css">
body {
background-color: #FFFFFF;
}
</style>
<BR>
<BR>
<BR>
<div align="center">
<p><img src="https://cdn.pixabay.com/photo/2017/04/25/22/28/despaired-2261021_960_720.jpg" width="50%" height="50%"></p>
<table width="459" border="0">
<tbody>
<tr>
<td width="462"><div align="center">
<hr>
</div>
<form id="myForm" align="center">
<input type="text" name="myName" placeholder="NOM_Prenom">
<input type="file" name="myFile">
<input type="submit" value="Envoyer le fichier" 
onclick="this.value='Uploading..';
google.script.run.withSuccessHandler(fileUploaded)
.uploadFiles(this.parentNode);
return false;">
</form>
<div id="output"></div>
<script>
function fileUploaded(status) {
document.getElementById('myForm').style.display = 'none';
document.getElementById('output').innerHTML = status;
}
</script>
<style>
input { display:block; margin: 20px; }
</style>
<hr></td>
</tr>
</tbody>
</table>
<h3>&nbsp;</h3>
<p>&nbsp;</p>
</div>

非常感谢!:(

使用this.parentNode上传并不总是适用于所有文件类型

相反,Filereader提供了一个优雅可靠的上传过程。

样本

代码.gs:

function doGet(e) {
return HtmlService.createHtmlOutputFromFile('form.html');
}
function uploadFiles(blob, name, description) {
try {
var dropbox = "Travaux_T°S";
var folder, folders = DriveApp.getFoldersByName(dropbox);
if (folders.hasNext()) {
folder = folders.next();
} else {
folder = DriveApp.createFolder(dropbox);
}
var blob = blob.split(",")
var blob = Utilities.newBlob(Utilities.base64Decode(blob[1]), 'application/pdf');
console.log(blob);
var fileName = blob.setName(name).getName(); 
var file = folder.createFile(blob); 
file.setDescription("Uploaded by " + description);    
return "Et hop: une copie de plus à corriger ! :)" ;
} catch (error) {
return error.toString();
} 
}

form.html:

<!doctype html>
<style type="text/css">
body {
background-color: #FFFFFF;
}
</style>
<BR>
<BR>
<BR>
<div align="center">
<p><img src="https://cdn.pixabay.com/photo/2017/04/25/22/28/despaired-2261021_960_720.jpg" width="50%" height="50%"></p>
<table width="459" border="0">
<tbody>
<tr>
<td width="462"><div align="center">
<hr>
</div>
<form id="myForm" align="center">
<input type="text" name="myName" placeholder="NOM_Prenom">
<input type="file" name="myFile">
<input type="submit" value="Envoyer le fichier" 
onclick="upload()" >
</form>
<div id="output"></div>
<script>   
function upload() {
var file = document.getElementsByName('myFile')[0].files[0];
var description = document.getElementsByName('myName')[0].value;
var reader = new FileReader();
reader.onload = function (e) {
var content = reader.result;
console.log('Sending ' + file.name);
var currFolder = 'Google_Dropbox';
google.script.run.withSuccessHandler(fileUploaded).uploadFiles(content, file.name, description);
return false;
}
reader.readAsDataURL(file);
}   
function fileUploaded(status) {
document.getElementById('myForm').style.display = 'none';
document.getElementById('output').innerHTML = status;
}    
</script>
<style>
input { display:block; margin: 20px; }
</style>
<hr></td>
</tr>
</tbody>
</table>
<h3>&nbsp;</h3>
<p>&nbsp;</p>
</div>

相关内容

  • 没有找到相关文章

最新更新