Last active
October 19, 2021 03:18
-
-
Save Mirochiu/02c79cacfa44b4bf33aba8cf70e42b93 to your computer and use it in GitHub Desktop.
sample to upload html file in appscript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="zh-Hant-TW"> | |
<head><base target="_top"><meta charset="UTF-8"></head> | |
<body> | |
<h1>上傳HTML檔案</h1> | |
<form id="upload_form" onsubmit="onSubmit(event)"> | |
<label for="html_name">網頁名稱</label> | |
<input id="html_name" type="text" name="the-name" required /> | |
<br /> | |
<label for="file_chooser">請選檔案</label> | |
<input id="file_chooser" type="file" name="the-file" accept="text/html" /> | |
<br /> | |
<input type="submit" value="上傳" id="upload" /> | |
</form> | |
<div id="msg"></div> | |
<br /> | |
<script> | |
function showText(txt) { | |
var target = document.getElementById('msg'); | |
if (target) { | |
target.textContent = typeof txt !== "string" ? JSON.stringify(txt) : txt; | |
} | |
} | |
function onSubmit(event) { | |
event.preventDefault(); | |
showText('上傳中,請稍候...'); | |
try { | |
google.script.run | |
.withSuccessHandler(onCompleted) | |
.withFailureHandler(onFailure) | |
.uploadHtmlFile(event.target); | |
} catch (error) { | |
onFailure(error); | |
} | |
return false; | |
} | |
function onCompleted(response) { | |
var target = document.getElementById('msg'); | |
if (target) { | |
target.innerHTML = ''; | |
target.appendChild(document.createTextNode('上傳成功:')); | |
target.appendChild(createLink(response.url, response.name)); | |
} | |
function createLink(url, name) { | |
var a = document.createElement('a'); | |
a.appendChild(document.createTextNode(name || url)); | |
a.href = url; | |
return a; | |
} | |
} | |
function onFailure(error) { | |
console.error(error); | |
showText('上傳失敗,錯誤訊息:' + error); | |
} | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment