Last active
March 5, 2018 14:08
-
-
Save MdKutubuddinSardar/fe68c1cf120a23f332e40aed2de73fac to your computer and use it in GitHub Desktop.
Uploading Local Files to Google Drive without Authorization using HTML Form
This file contains 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
function doPost(e) { | |
var data = Utilities.base64Decode(e.parameters.data); | |
var blob = Utilities.newBlob(data, e.parameters.mimetype, e.parameters.filename); | |
DriveApp.createFile(blob); | |
return ContentService.createTextOutput("Done.") | |
} |
This file contains 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> | |
<head> | |
<title>Sample script for uploading file to Google Drive without authorization</title> | |
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.js"></script> | |
</head> | |
<body> | |
<form action="https://script.google.com/macros/s/#####/exec" id="form" method="post"> | |
Upload a file | |
<div id="data"></div> | |
<input name="file" id="uploadfile" type="file"> | |
<input id="submit" type="submit"> | |
</form> | |
<script> | |
$('#uploadfile').on("change", function() { | |
var file = this.files[0]; | |
var fr = new FileReader(); | |
fr.fileName = file.name | |
fr.onload = function(e) { | |
e.target.result | |
html = '<input type="hidden" name="data" value="' + e.target.result.replace(/^.*,/, '') + '" >'; | |
html += '<input type="hidden" name="mimetype" value="' + e.target.result.match(/^.*(?=;)/)[0] + '" >'; | |
html += '<input type="hidden" name="filename" value="' + e.target.fileName + '" >'; | |
$("#data").empty().append(html); | |
} | |
fr.readAsDataURL(file); | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is a sample script for uploading local file to Google Drive without the authorization using HTML form. A selected file in your local PC using HTML form is uploaded to Google Drive and saved to Google Drive.
When you use this, at first, please deploy Web Apps. The script is doPost() of following scripts.
Flow :
Script : HTML
https://script.google.com/macros/s/#####/exec is the URL obtained when the Web Apps was deployed. Please replace it to your Web Apps URL. You can open this HTML for the browser of your local PC.
Flow :
Note :