0

I have found a video that explains how to upload files to Google Drive and here's the video link https://www.youtube.com/watch?v=l9atSDs7-oI And here's the script used

function uploadFiles(url) { var response = UrlFetchApp.fetch(url) var fileName = getFilenameFromURL(url) var folder = DriveApp.getFolderById('1IxMiswEfi67ovoBf8ZH1RV7qVPx1Ks6l'); var blob = response.getBlob(); var file = folder.createFile(blob) file.setName(fileName) file.setDescription("Download from the " + url) return file.getUrl(); } function getFilenameFromURL(url) { //(host-ish)/(path-ish/)(filename) var re = /^https?:\/\/([^\/]+)\/([^?]*\/)?([^\/?]+)/; var match = re.exec(url); if (match) { return unescape(match[3]); } return null; } function doGet(e){ var html = HtmlService.createHtmlOutputFromFile('index.html') return html.setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL) } 

Create index.html on the Google app project and put the below code

<!DOCTYPE html> <html> <head> <base target="_top"> <title>Upload Files</title> </head> <body> <h1>Upload files to Google drive from URL</h1> <form> <label>Enter the URL</label> <input type="text" name="myFile" id="url" style="height:5%; width:70%"> <br> <br> <input type="button" id="submitBtn" value="Upload Files"> <label id="resp"><label> </form> <script> document.getElementById('submitBtn').addEventListener('click', function(e){ var url= document.getElementById("url").value; google.script.run.withSuccessHandler(onSuccess).uploadFiles(url) }) function onSuccess(url){ document.getElementById('resp').innerHTML = "File uploaded to path" + url; } </script> </body> </html> 

I have changed the folder id in the code. When I tried to use that, I encountered an error Uncaught at uploadFiles (Code:7).

4
  • 2
    I think that your script works when both the inputted URL and the folder ID of 1IxMiswEfi67ovoBf8ZH1RV7qVPx1Ks6l is correct. So, for example, when you redeploy the Web Apps as new version and test it again, what result will you get? If you use the URL of https://script.google.com/macros/s/###/exec, when the script of Web Apps is modified, the latest script is reflected to the Web Apps by redeploying the Web Apps as new version. I'm worry about this. Ref Commented Aug 23, 2020 at 8:25
  • And also, if the line 7 of your error message of Uncaught at uploadFiles (Code:7) is file.setName(fileName) in the function uploadFiles, can you show the value of fileName? In that case, for example, as a test case, when you modified it to file.setName("sample"), what result will you get? Commented Aug 23, 2020 at 8:59
  • Thank you very much. You are right. The problem was related to publish (redeploying the Web Apps as new version solved the problem) Commented Aug 23, 2020 at 13:00
  • 1
    Thanks a lot. Please put your reply as the answer of yours so as to mark it (You deserve that). Commented Aug 24, 2020 at 5:05

1 Answer 1

4

I think that your script works when both the inputted URL and the folder ID is correct in your script. From this situation, please check the following point.

  • At the Web Apps of Google Apps script, when you use the URL of https://script.google.com/macros/s/###/exec, when the script of Web Apps is modified, the latest script is reflected to the Web Apps by redeploying the Web Apps as new version.

This is the important point for using the Web Apps of Google Apps Script. So I would like to propose to confirm above.

References:

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.