我正在创建一个Google Web App(HTML表单),它会将文件上传到My Drive上的文件夹。不需要上传文件,因此有时候这个输入基本上是“空白”。除非您没有选择要上传的文件,否则该应用程序可以正常运行。它吐出了这个错误:“例外:我们很抱歉,发生了服务器错误。请稍等一下再试一次。”我有两个文件,html文件和.gs文件。这是他们:
/* The script is deployed as a web app and renders the form */
function doGet(e) {
return HtmlService.createHtmlOutputFromFile('FormFrontend.html');
}
/* This function will process the submitted form */
function uploadFiles(form) {
try {
/* Name of the Drive folder where the files should be saved */
var dropfolder = "Uploaded Files";
var folder, folders = DriveApp.getFoldersByName(dropfolder);
/* Find the folder, create the folder if it does not exist */
if (folders.hasNext()) {
folder = folders.next();
} else {
folder = DriveApp.createFolder(dropfolder);
}
/* Get the file uploaded though the form as a blob */
var blob = form.myFile;
var file = folder.createFile(blob);
var urlstr = file.getUrl()
/* Set the file description as the name of the uploader */
file.setDescription("Uploaded by " + form.ContactName);
/* Write response to spreadsheet */
var ss = SpreadsheetApp.openById("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
var responses = ss.getSheetByName("Responses");
responses.appendRow([form.CompanyName, form.ContactName, form.PhoneNumber, form.Email, form.Date, form.Severity, form.Details, urlstr])
/* As long as there's no errors you should se the below text */
return "Form Submitted Successfully "
} catch (error) {
/* If there's an error, show the error message */
return error.toString();
}
}
<html>
<body>
<!-- This is the actual HTML form -->
<div id="theform">
<form id="myForm">
<p style="font-size:30px">Customer Form</p>
Company Name:
<input type="text" name="CompanyName">
<br>Contact Name:
<input type="text" name="ContactName">
<br>Phone Number:
<input type="text" name="PhoneNumber">
<br>Contact Email:
<input type="email" name="Email">
<br>Date:
<input type="date" name="Date">
<br>Overall Severity: (1 Lowest, 5 Highest)
<br>
<input type="number" name="Severity" min="1" max="5" value="1">
<br>Details:
<br>
<textarea name="Details" rows=10 cols=65></textarea>
<br>
<br>Additional File (Optional):
<input type="file" name="myFile">
<br>
<!-- The submit button. It calls the server side function uploadFiles() on click -->
<input type="submit" value="Submit" onclick="this.value='Submitting..';
google.script.run.withSuccessHandler(fileUploaded)
.uploadFiles(this.parentNode);
return false;">
</form>
</div>
<!-- Here the results of the form submission will be displayed -->
<div id="output"></div>
</body>
</html>
<!-- The function will be called after the Google Script has executed -->
<script>
function fileUploaded(status) {
document.getElementById('myForm').style.display = 'none';
document.getElementById('output').innerHTML = status;
}
</script>
如果尝试在“/ *获取通过表单上传的文件作为blob * /”部分放置一个IF语句,告诉它只要将form.myFile空白时将urlstr设置为空,但它仍然失败(但如果你选择一个实际的文件,它仍然成功完成)。我无法在记录器中显示任何有用的信息。
我是Google App脚本的新手,所以任何帮助都会受到赞赏!
答案 0 :(得分:0)
我会检查实际返回到变量blob
的内容。
var blob = form.myFile;
也许使用JavaScript typeOf
检查类型。
var whatsTheType = typeOf blob;
在此测试功能中:
function testIt() {
var newTestFile = DriveApp.createFile('New Text File', 'Hello, world!');
var myBlob = newTestFile.getBlob();
var whatsTheType = typeof myBlob;
Logger.log('whatsTheType: ' + whatsTheType);
}
JavaScript typeof
将blob的类型作为“对象”返回。如果您检查typeof
,并且它不是对象,则表示该文件未上传。
进行if
条件检查,而不是“尝试/捕获”。 “尝试/捕获”显然不会阻止代码死亡。
if (whatsTheType === "object") {
//Create the file
var file = folder.createFile(blob);
};
或者,如果文件已上传,您应该能够检查文件选择器的value
属性以返回名称。如果上传的文件没有名称,则用户不使用文件选择器。
答案 1 :(得分:0)
这就是最终的工作。 .getContentType似乎总是返回“application / octet-stream”,当它留空并检查返回的内容类型是否是特定的。
/* Get the file uploaded though the form as a blob */
var blob = form.myFile;
var contentType = blob.getContentType();
if (contentType != "application/octet-stream") {
var file = folder.createFile(blob);
var urlstr = file.getUrl();
/* Set the file description as the name of the uploader */
file.setDescription("Uploaded by " + form.ContactName);
} else {
var urlStr = "None given";
}