1.Code.gs
function doGet(e) {
return HtmlService.createHtmlOutputFromFile('Index');
}
function doPost(e) {
if (!e.parameters.filename || !e.parameters.file || !e.parameters.fileformat) {
return message("Error: Bad parameters");
} else {
var mime =e.parameters.fileformat;
if (mime) {
var data = Utilities.base64Decode(e.parameters.file, Utilities.Charset.UTF_8);
var blob = Utilities.newBlob(data, mime, e.parameters.filename);
DriveApp.getFolderById('0B86b-ALn-1MGTEI1MjJ4SWFxSDQ').createFile(blob);
return message("completed");
} else {
return message("Error: Bad image format");
}
}
}
function message(msg) {
return ContentService.createTextOutput(JSON.stringify({result: msg})).setMimeType(ContentService.MimeType.JSON);
}
2.Index.html
<!DOCTYPE html><html>
<head>
<base target="_top">
<meta charset="UTF-8">
<script src="https://code.jquery.com/jquery.min.js"></script>
</head>
<body>
<input type="file" id="file">
<script type="text/javascript">
$(function(){
var projectid="AKfycbypZxDkD2Br3dXDTieD2zWS2lvqvn5Ur7gY52ivcNg_TdK8a2V8";
var url = 'https://script.google.com/macros/s/'+projectid+'/exec';
var params = {
filename: 'test',
fileformat: 'txt'
};
$('#file').on("change", function() {
var file = this.files[0];
//console.dir(file);
/*example
lastModified:1491349967000
lastModifiedDate:Wed Apr 05 2017 07:52:47 GMT+0800 (MYT)
name:"43e4af3c-36a5-47bf-bfec-658504d9de33.mp4"
size:67755
type:"video/mp4"
webkitRelativePath:""
*/
var fr = new FileReader();
fr.onload = function(e) {
params.file = e.target.result.replace(/^.*,/, '');
params.filename=file.name.split('.')[0];
params.fileformat=file.type;
postJump();
}
fr.readAsDataURL(file);
});
function postJump(){
var html = '<form method="post" action="'+url+'" id="postjump" style="display: none;">';
Object.keys(params).forEach(function (key) {
html += '<input type="hidden" name="'+key+'" value="'+params[key]+'" >';
});
html += '</form>';
$("body").append(html);
$('#postjump').submit();
$('#postjump').remove();
}
});
</script>
</body>
</html>
0 Comments:
Post a Comment