Skip to content Skip to sidebar Skip to footer

How To Retrieve Absolute Link Of Google Drive File From Return Statement?

I have a html form and Im uploading a media file to google drive and return the file. This is my code:

Solution 1:

I think that the file type is kept on the Google Drive. But when the file is downloaded, I thought that the file type might be not known because of no extension. So how about adding the extension when the file is uploaded?

When your script is modified, it becomes as follows.

Modified script:

From:
fr.onload = f => {
  
  const url = "https://script.google.com/macros/s/###/exec";  // <--- Please set the URL of Web Apps.
  
  const qs = new URLSearchParams({filename: form.filename.value || file.name, mimeType: file.type});
  fetch(`${url}?${qs}`, {method: "POST", body: JSON.stringify([...new Int8Array(f.target.result)])})
  .then(res => res.json())
  .then(e => console.log("https://drive.google.com/uc?export=download&id=" + e.fileId))
  .catch(err => console.log(err));
}
To:
fr.onload = f => {
  // I added below script.
  let newName = form.filename.value;
  const orgName = file.name;
  if (orgName.includes(".")) {
    const orgExt = orgName.split(".").pop();
    if (orgExt != newName.split(".").pop()) {
      newName = newName ? `${newName}.${orgExt}` : orgName;
    }
  }
  
  const url = "https://script.google.com/macros/s/###/exec";
  
  const qs = new URLSearchParams({filename: newName, mimeType: file.type});  // Modified
  fetch(`${url}?${qs}`, {method: "POST", body: JSON.stringify([...new Int8Array(f.target.result)])})
  .then(res => res.json())
  .then(e => console.log(e.fileUrl))  // <--- You can retrieve the returned value here.
  .catch(err => console.log(err));
}

Post a Comment for "How To Retrieve Absolute Link Of Google Drive File From Return Statement?"