- Когда вы нажимаете кнопку «Отправить»,
- Форма Google работает нормально.
document.getElementById('status').style.display = 'inline'
работает. - Функция
processForm(frmData)
в Службах GoogleСкрипт не работает. - Функция
updateOutput()
в Javascript не работает.
Если мое понимание верно, как насчет этой модификации?Пожалуйста, подумайте об этом как об одном из нескольких ответов.
Точки модификации:
- В этой модификации файл был получен с использованием события
onclick
, а сценарий для отправки в GoogleФорма не изменяется. - Полученный файл преобразуется в base64 и отправляется в Google Apps Script.
Измененный сценарий:
HTML:
<form id="myForm" action="https://docs.google.com/forms/d/e/xxx/formResponse" target="hidden_iframe" onsubmit="submitted=true;">
<input placeholder="1234" name="entry.1234" id="user" type="text">
<label for="user">User:</label>
<input name="picToLoad" type="file" id="sampleFile" /> <!-- Modified -->
<div id="status" style="display: none">
Uploading. Please wait...
</div>
<button type="submit" name="action" id="sampleId" >Send</button> <!-- Modified -->
</form>
Javascript:
<script>
// Below script was added.
document.getElementById("sampleId").onclick = function(e) {
e.stopPropagation();
e.preventDefault();
var file = document.getElementById("sampleFile").files[0];
const f = new FileReader();
f.onload = (e) => {
const data = e.target.result.split(",");
const obj = {fileName: file.name, mimeType: data[0].match(/:(\w.+);/)[1], data: data[1]};
picUploadJs(obj);
}
f.readAsDataURL(file);
};
function picUploadJs(frmData) {
document.getElementById('status').style.display = 'inline';
google.script.run.withSuccessHandler(updateOutput).processForm(frmData)
};
function updateOutput() {
var outputDiv = document.getElementById('status');
outputDiv.innerHTML = "The File was UPLOADED!";
window.location='https://thankyoupage/';
}
</script>
Скрипт Google Apps:
function processForm(theForm) {
var fileBlob = Utilities.newBlob(Utilities.base64Decode(theForm.data), theForm.mimeType, theForm.fileName);
var fldrSssn = DriveApp.getFolderById('xxxx');
fldrSssn.createFile(fileBlob);
return true;
}
Примечание:
- Когда файл выбран и нажмите «Отправить»Кнопка, файл отправляется в Google Apps Script и создается как файл на Google Диске, а форма Google отправляется.Затем запускается
updateOutput()
на стороне Javascript. - В этом измененном сценарии используется BLOB-объект.Таким образом, максимальный размер файла для загрузки составляет 50 МБ.Пожалуйста, будьте осторожны с этим.
Edit 1:
При вашем комментарии было обнаружено, что When I remove the id=sampleId from the submit button, the Google Form data is submitted correctly
.Используя это, пожалуйста, проверьте следующую модификацию.
В этой модификации id="sampleId"
была удалена, и событие вызывается с использованием имени элемента.
HTML:
<form id="myForm" target="hidden_iframe" onsubmit="submitted=true;">
<input placeholder="1234" name="entry.1234" id="user" type="text">
<label for="user">User:</label>
<input name="picToLoad" type="file" id="sampleFile" />
<div id="status" style="display: none">
Uploading. Please wait...
</div>
<button type="submit" name="action">Send</button> <!-- Modified -->
</form>
Javascript:
var button = document.getElementsByName('action')[0]; // Modified
button.onclick = function(e) { // Modified
e.stopPropagation();
e.preventDefault();
var file = document.getElementById("sampleFile").files[0];
const f = new FileReader();
f.onload = (e) => {
const data = e.target.result.split(",");
const obj = {fileName: file.name, mimeType: data[0].match(/:(\w.+);/)[1], data: data[1]};
picUploadJs(obj);
}
f.readAsDataURL(file);
};
Редактировать 2:
Я обновил HTML и Javascript.Пожалуйста, подтвердите.Сценарий Google Apps не изменяется.
HTML:
<iframe name="hidden_iframe" id="hidden_iframe" style="display:none;" onload="if(submitted) { picUploadJs(myForm); }"></iframe>
<form id="myForm" action="https://docs.google.com/forms/d/e/xxx/formResponse" target="hidden_iframe" onsubmit="submitted=true;">
<input placeholder="1234" name="entry.1234" id="user" type="text">
<label for="user">User:</label>
<input name="picToLoad" type="file" id="sampleFile" /> <!-- Modified -->
<div id="status" style="display: none">
Uploading. Please wait...
</div>
<button type="submit" name="action">Send</button>
</form>
Javascript:
<script>
// This function was modified.
function picUploadJs(myForm) {
var file = document.getElementById("sampleFile").files[0];
const f = new FileReader();
f.onload = (e) => {
const data = e.target.result.split(",");
const obj = {fileName: file.name, mimeType: data[0].match(/:(\w.+);/)[1], data: data[1]};
document.getElementById('status').style.display = 'inline';
google.script.run.withSuccessHandler(updateOutput).processForm(obj);
}
f.readAsDataURL(file);
}
function updateOutput() {
var outputDiv = document.getElementById('status');
outputDiv.innerHTML = "The File was UPLOADED!";
window.location='https://thankyoupage/';
}
</script>