function doPost(e) {
const folderId = "###"; // Folder ID which is used for putting the file.
const blob = Utilities.newBlob(
JSON.parse(e.postData.contents),
e.parameter.mimeType,
e.parameter.filename
);
const file = DriveApp.getFolderById(folderId || "root").createFile(blob);
const responseObj = {
filename: file.getName(),
fileId: file.getId(),
fileUrl: file.getUrl(),
};
return ContentService.createTextOutput(
JSON.stringify(responseObj)
).setMimeType(ContentService.MimeType.JSON);
}function doPost(e) {
const folderId = "###"; // Folder ID which is used for putting the file.
const blob = Utilities.newBlob(
JSON.parse(e.postData.contents),
e.parameter.mimeType,
e.parameter.filename
);
const file = DriveApp.getFolderById(folderId || "root").createFile(blob);
const responseObj = {
filename: file.getName(),
fileId: file.getId(),
fileUrl: file.getUrl(),
};
return ContentService.createTextOutput(
JSON.stringify(responseObj)
).setMimeType(ContentService.MimeType.JSON);
}
<form id="form">
<input name="file" id="uploadfile" type="file" />
<input id="submit" type="submit" />
</form>
<script>
const form = document.getElementById("form");
form.addEventListener("submit", (e) => {
e.preventDefault();
const file = form.file.files[0];
const fr = new FileReader();
fr.readAsArrayBuffer(file);
fr.onload = (f) => {
const url = "https://script.google.com/macros/s/###/exec"; // Please set the URL of Web Apps.
const qs = new URLSearchParams({
filename: file.name,
mimeType: file.type,
});
fetch(`${url}?${qs}`, {
method: "POST",
body: JSON.stringify([...new Int8Array(f.target.result)]),
})
.then((res) => res.json())
.then(console.log)
.catch(console.log);
};
});
</script>