I am trying to build a script to save data (text + image + pdf) from an HTML form (NOT Google Form).
I have the following function to upload a file:
function upload(x) {
var destination_id = 'folder_id'
var destination = DriveApp.getFolderById(destination_id)
var contentType = 'image/png'
var img = x.getAs(contentType)
destination.createFile(img)
}
Inside my doPost
function I am calling the above upload function like this:
upload(e.parameter.img)
I have an input of type file
with a name and id of img
. I also have the following event listener to prepare the body of the POST request:
form.addEventListener('submit', e => {
e.preventDefault()
let fd = new FormData(form)
fetch(scriptURL, { method: 'POST', body: fd})
.then(response => console.log('Success!', response))
.catch(error => console.error('Error!', error.message))
})
When I submit my form, my text gets saved, but the file is not uploaded. I do not see any errors in the console either. What am I doing wrong? Is there an easier alternative to uploading form data to Google Drive?