google-apps-scriptfile-uploadweb-applicationsgoogle-drive-api

Error Uncaught at uploadFiles (Code:7) when trying to upload files


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).


Solution

  • 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.

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

    References: