【发布时间】:2015-07-30 16:21:00
【问题描述】:
我有一个要传递给应用程序脚本函数的表单。表单元素之一是允许用户进行多项选择的列表框。每当从列表框中选择一个项目时,该函数(从另一个输入元素上传图像)运行良好,但无论何时选择两个或更多项目,该函数都会引发失败。虽然该函数从不使用列表框,但是否有某些原因使其成为表单的一部分可能会导致将其传递给函数时出现问题,我该如何解决?
HTML sn-ps:
<form id="classForm">
<img id="previewImg" width=300px>
<input type="file" name="imageLoader" id="imageUpload" accept="image/*" onchange="upload();">
<select multiple="true">
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
</select>
</form>
Javascript:
<script type="text/javascript">
//This function stub passes in the form to a function that uploads a user-provided image for preview use
function upload(){
var form = document.getElementById("classForm");
google.script.run.withSuccessHandler(uploadResult).uploadFile(form);
google.script.run.withFailureHandler(uploadResult).uploadFile(form);
}
//This function displays a preview of the user's ad image
//Data in: id of image, to be used to generate url
function uploadResult(url){
url = "https://docs.google.com/uc?id=" + url;
$('#previewImg').attr('src', url);
}
//clears input so that onchange will fire even if user selects same image
$(document).on("click", "#imageUpload", function() {
this.value = null;
});
</script>
函数(从上传调用,它做一些处理,然后在表单中发送):
function uploadFile(form) {
// Name of folder where the files should be saved
var folderName = "AdImages";
var folder, folders = DriveApp.getFoldersByName(folderName);
//Find the folder, create if the folder does not exist
if (folders.hasNext()) {
folder = folders.next();
} else {
folder = DriveApp.createFolder(folderName);
}
// Get file from form as blob
var blob = form.imageLoader;
var file = folder.createFile(blob);
// Set the file description to something
file.setDescription("title");
//get picID
var picId = file.getId();
// Return the id of the picture, to be used to construct the previewable url
return picId;
}
【问题讨论】:
-
错误信息是什么?多选将给出一个以逗号分隔的值列表,因此您需要处理它 - 但
upload()函数在做什么?您的 sn-ps 不足以重现问题,因此很难为您提供帮助。 minimal reproducible example -
很抱歉——我重建了一个最小版本的程序来重现问题并更新了上面的细节。我查看了日志并没有出现任何错误消息,但它仍然像抛出 withFailureHandler 一样运行。
-
我建议将您的 uploadFile() 函数封装在 try-catch 语句中并记录消息,以便您可以准确地看到导致错误的原因。
-
我收到的确切错误是 NetworkError: Form submit failed。
标签: javascript forms google-apps-script