Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save erajanraja24/8d8cd08a86873d8204734bef32fa54a2 to your computer and use it in GitHub Desktop.
Save erajanraja24/8d8cd08a86873d8204734bef32fa54a2 to your computer and use it in GitHub Desktop.
Upload files from Blog/Website to Google Drive
//Google Apps Script. Need to be pasted on the code.gs file
function doGet() {
var html = HtmlService.createHtmlOutputFromFile('index');
return html.setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL);
}
function uploadFiles(data)
{
var file = data.myFile;
var folder = DriveApp.getFolderById('1IxMiswEfi67ovoBf8ZH1RV7qVPx1Ks6l');
var createFile = folder.createFile(file);
return createFile.getUrl();
}
//HTML. Need to be pasted on the index.html
<!DOCTYPE html>
<html>
<head>
<base target="_top">
<title>Upload Files</title>
</head>
<body>
<h1>File Uploader</h1>
<form>
<input type="file" name="myFile" mulitple>
<br>
<br>
<input type="button" id="submitBtn" value="Upload Files">
<label id="resp"></label>
</form>
<script>
document.getElementById('submitBtn').addEventListener('click',
function(e){
google.script.run.withSuccessHandler(onSuccess).uploadFiles(this.parentNode)
})
function onSuccess(data){
document.getElementById('resp').innerHTML = "File Uploaded to the path " +data;
}
</script>
</body>
</html>
@wasankds
Copy link

Good !!!

@hatikva
Copy link

hatikva commented Dec 2, 2019

thanks alot

@sbr-1979
Copy link

sbr-1979 commented Jan 9, 2020

Syntax error. (line 39, file "Code")Dismiss

@protheusdk
Copy link

protheusdk commented Feb 6, 2020

you should delete this word "mulitple" in your code, its a typo, but also because if you select 2 files only one is uploaded, thanks for share your code, it really helped me.

@pwstorey
Copy link

pwstorey commented Feb 24, 2020

Hi, I'm trying to incorporate this into another larger form. The "this.parentNode" argument seems to be causing issues. Is there a way to get the file data from the input type="file" id="fileUpload element using id?

@pratikdebbarman
Copy link

i need a help your code is working in uploading any files and after uploadation only text file seems to open other files is not opening in google drive. what changes i need to do.

@pratikdebbarman
Copy link

this the ".png" file which i have uploaded to drive folder and it was uploaded successfully but while uploading the file got corrupted and hence i cannot view the file in google drive folder.
Screenshot (1)

@Mizu-Ma
Copy link

Mizu-Ma commented Jul 15, 2020

I am having the same issue. The file seems to be corrupted after the upload

@coldes
Copy link

coldes commented Jul 26, 2020

Make sure to use Rhino JavaScript interpreter: Run->Disable new Apps Script runtime powered by Chrome V8.
https://stackoverflow.com/questions/60959432/gs-upload-file-is-corrupted

@pradeepa1511
Copy link

It shows a TypeError: Cannot read property 'myFile' of undefined (line 11, file "Code")

@thorstenmerl
Copy link

Thank you erajanraja24! It works perfectly (I deleted the word "mulitple")

@Roma1972er
Copy link

the upload works perfect, but i cant view the picture on google drive or if i download it later. the size on my computer and to drive or later back is also different. When i use "createFile" oder "makeCopy" without WebApp everything works fine.
So the file with the the right name will created on the drive but something going wrong by transmit the file.
Any ideas what i'm doing wrong - i use the code shown

@gavrss
Copy link

gavrss commented May 23, 2021

the upload works perfect, but i cant view the picture on google drive or if i download it later. the size on my computer and to drive or later back is also different. When i use "createFile" oder "makeCopy" without WebApp everything works fine.
So the file with the the right name will created on the drive but something going wrong by transmit the file.
Any ideas what i'm doing wrong - i use the code shown

I have the same: uploaded file has doubled size against my local file on computer. And this file is corrupted for view.
Who can fix it?

@gavrss
Copy link

gavrss commented May 23, 2021

the upload works perfect, but i cant view the picture on google drive or if i download it later. the size on my computer and to drive or later back is also different. When i use "createFile" oder "makeCopy" without WebApp everything works fine.
So the file with the the right name will created on the drive but something going wrong by transmit the file.
Any ideas what i'm doing wrong - i use the code shown

I have the same: uploaded file has doubled size against my local file on computer. And this file is corrupted for view.
Who can fix it?

Sorry, this is right solution:
https://gist.github.com/erajanraja24/8d8cd08a86873d8204734bef32fa54a2#gistcomment-3391596
Make sure to use Rhino JavaScript interpreter: Run->Disable new Apps Script runtime powered by Chrome V8.
https://stackoverflow.com/questions/60959432/gs-upload-file-is-corrupted

@truongtv2021
Copy link

Thank you so much!

@Hasnisubhan
Copy link

I can't load the web page on mobile device.

@anyasohaj
Copy link

Thanks a lot, you've saved my life :)

@sohan110111
Copy link

Thanks brother

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment