Fabio Quarti
Fabio Quarti

Reputation: 41

Download web image and save it in a specific drive folder

I need to download an image with GS and save it in a specific drive folder.
I'm able to save the image in the root folder but I cannot save it in a specific folder:

function downloadFile(fileURL,folder) {  

  var fileName = "";
  var fileSize = 0;
  
  var response = UrlFetchApp.fetch(fileURL, {muteHttpExceptions: true});
  var rc = response.getResponseCode();
  
  if (rc == 200) {
    var fileBlob = response.getBlob()
    var folder = DriveApp.getFoldersByName(folder);
    if (folder != null) {
      var file = DriveApp.createFile(fileBlob);
      fileName = file.getName();
      fileSize = file.getSize();
    }
  }
    
  var fileInfo = { "rc":rc, "fileName":fileName, "fileSize":fileSize };
  return fileInfo;
}

Question: what have I to add to use the variable "folder"?

I found a lot of examples with "DocList" Class that is not in use anymore

Upvotes: 4

Views: 8147

Answers (3)

Trung Luong
Trung Luong

Reputation: 21

Well, I guess GAS has make a lot of progress on developing its API, the function createFile(blob) of an object Folder will do the job: https://developers.google.com/apps-script/reference/drive/folder#createfileblob

// Create an image file in Google Drive using the Maps service.
var blob = Maps.newStaticMap().setCenter('76 9th Avenue, New York NY').getBlob();
DriveApp.getRootFolder().createFile(blob);

It's quite late for the answer but just incase some one runs into the situation.

Upvotes: 2

Nick Johnston
Nick Johnston

Reputation: 1

Working on similar problem, I came up with the solution below to save a file to a folder. If the folder doesn't exist it creates it, otherwise it saves the file specified by "FOLDER_NAME"

  var folderExists = checkFolderExists("FOLDER_NAME");

  if (folderExists) {
     saveFolder = DriveApp.getFolderById(folderExists);
  } else {
     saveFolder = DriveApp.createFolder("FOLDER_NAME");
  }

  // Make a copy of the file in the root drive.
  var file = DriveApp.getFileById(sheetID);
  // Take the copy of the file created above and move it into the folder:
  var newFile = DriveApp.getFolderById(saveFolder.getId()).addFile(file);

  // Remove the copy of the file in the root drive.
  var docfile = file.getParents().next().removeFile(file);

Further to Eric's answer, I have also provided a utility function that checks if the folder exists. It's reusable in any project.

function checkFolderExists(fName) {
  try {
    var folderId;
    var folders = DriveApp.getFolders();

    while (folders.hasNext()) {
      var folder = folders.next();
      folderName = folder.getName();
      if (folderName == fName) {
         folderId = folder.getId();
      }
    }
  } catch(e) {
    log("Services::checkFolderExists()" + e.toString());
    throw e;
  }  
  return folderId;
 }

Upvotes: 0

Eric Dauenhauer
Eric Dauenhauer

Reputation: 759

Are you familiar with this app? It does exactly what you're asking for.

However, if you want to re-create this for your own purposes, I would change your declaration of variable file to read as such:

var file = folder.next().createFile(fileBlob);

when you create your variable folder, the method you use creates a FolderIterator, not a single folder. You have to call the next() method to get a Folder object.

To be precise with your script and avoid saving to an incorrect-but-similarly-named folder, I would recommend passing the folder ID to your script rather than the folder Name. If you pass the folder ID, you could declare folder as:

var folder = DriveApp.getFolderById(folder);

and then continue the script as you have it written. I hope that helps.

Upvotes: 1

Related Questions