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).
1IxMiswEfi67ovoBf8ZH1RV7qVPx1Ks6lis 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 ofhttps://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. Ref7of your error message ofUncaught at uploadFiles (Code:7)isfile.setName(fileName)in the functionuploadFiles, can you show the value offileName? In that case, for example, as a test case, when you modified it tofile.setName("sample"), what result will you get?