Ben
Ben

Reputation: 289

How to get direct link to file uploaded via form?

I am using Google Apps Script to upload a file to a folder in my Google Drive. Is it possible, in apps script, to get a direct link to a file that I had just uploaded? Currently, I am only able to get a link to the folder where I am saving all my files.

Code I am using to upload files:

function processForm(theForm) {
  try{
    var fileBlob1 = theForm.resumeFile;
    var fileBlob2 = theForm.piFile;

    var fldrSssn = DriveApp.getFolderById('My Folder ID');
    fldrSssn.createFile(fileBlob1);
    fldrSssn.createFile(fileBlob2);
    return fldrSssn.getUrl();

  }catch(e) {
      Logger.log(e);
  }//End catch
}

Upvotes: 2

Views: 555

Answers (1)

Mogsdad
Mogsdad

Reputation: 45730

You first need to keep the File object that is returned by Folder.createFile(), then use that to retrieve the file url.

function processForm(theForm) {
  try{
    var fileBlob1 = theForm.resumeFile;
    var fileBlob2 = theForm.piFile;

    var fldrSssn = DriveApp.getFolderById('My Folder ID');
    var file1 = fldrSssn.createFile(fileBlob1);
    var file2 = fldrSssn.createFile(fileBlob2);

    // Return all URLs in a single object
    return { 
      folderURL: fldrSssn.getUrl(),
      file1URL:  file1.getUrl(),
      file2URL:  file2.getUrl()
    };

  }catch(e) {
      Logger.log(e);
  }//End catch
}

Upvotes: 1

Related Questions