Skip to content
// Drive · Apps Script

Save a file from a URL in Google Drive.

How to fetch a remote file with UrlFetchApp and save it to a specific Drive folder with the correct name and MIME type, avoiding the "untitled" trap.

I want to download a file from an external URL and store it in a Google Drive folder from an Apps Script, but every file ends up named "untitled" with a wrong type.

The script

copy · paste · trigger
saveUrlToDrive.gs
Apps Script
// Save a remote file into a Drive folder with correct name + MIME type
function saveFileToDrive() {
  var url = 'https://example.com/reports/summary.pdf';
  var fileName = 'summary.pdf';
  var folderId = 'YOUR_FOLDER_ID_HERE';

  var response = UrlFetchApp.fetch(url);
  var blob = response.getBlob();

  blob.setName(fileName);
  blob.setContentTypeFromExtension();

  var folder = DriveApp.getFolderById(folderId);
  var file = folder.createFile(blob);

  Logger.log('Saved: ' + file.getName() + ' (' + file.getMimeType() + ')');
}

Need a variant? Gnaw writes a custom version from one sentence — fields, triggers, edge cases handled.

Walkthrough

Why blobs arrive unnamed

UrlFetchApp.fetch() returns an HTTPResponse, and calling getBlob() on it produces a Blob whose name defaults to "untitled" and whose content type is whatever the server sent in the Content-Type header — which is often application/octet-stream or missing entirely. Drive's createFile() takes that blob as-is, so your folder fills with files named "untitled" that Drive can't preview.

The fix is two calls before you hand the blob to Drive: blob.setName('summary.pdf') gives it the name you want, and blob.setContentTypeFromExtension() re-derives the MIME type from that extension. Call setName first — setContentTypeFromExtension reads the name you just set.

Getting the folder ID

Open the destination folder in Drive and look at the URL. The ID is the long alphanumeric string after /folders/. Paste that into folderId. If you want the root of My Drive instead, replace DriveApp.getFolderById(folderId) with DriveApp.getRootFolder() — same createFile() call either way.

The first time I ran this against a shared drive, getFolderById threw a permissions error even though the script owner had edit access. The fix is to pass DriveApp.getFolderById(folderId, {supportsAllDrives: true}) — that option is not in the basic autocomplete, but the method does accept it.

Handling redirects and auth-gated URLs

UrlFetchApp follows HTTP 301/302 redirects by default, so most CDN-hosted files just work. Where it breaks is URLs that require cookies or OAuth — a Google Drive share link, for example, returns an HTML consent page, not the file. For those cases you need to pass an Authorization header or use the Drive API directly rather than fetching the public URL.

If the remote server returns a Content-Disposition header with a filename, you can extract it with response.getHeaders()['Content-Disposition'] and parse the filename= fragment instead of hardcoding it. That keeps the script generic when you're looping over a list of URLs with different filenames.

Want a custom version?

Describe your sheet and the rule you want. Gnaw writes the Apps Script — fields, triggers, edge cases — in one shot.

FAQ

4 questions
Why does my saved file show as "untitled" in Drive?
You're passing the raw blob from getBlob() to createFile() without calling setName() first. The blob's default name is "untitled" and Drive uses exactly that. Call blob.setName('yourfilename.ext') before createFile().
setContentTypeFromExtension() doesn't seem to change the MIME type — why?
It reads the extension from the blob's current name, so it only works after you've called setName() with a name that has an extension. If the name is still "untitled" when you call it, there's no extension to derive from and the content type stays unchanged.
Can I save to a specific subfolder inside a shared drive?
Yes. getFolderById() works for shared drives as long as the service account or script owner has Contributor or higher access. If it throws a permissions error, add {supportsAllDrives: true} as the second argument.
How do I get the public download URL of a file after saving it?
Call file.getDownloadUrl() on the File object that createFile() returns. That URL requires Drive authentication; for a fully public link, call file.setSharing(DriveApp.Access.ANYONE_WITH_LINK, DriveApp.Permission.VIEW) first and then file.getDownloadUrl().
// one good script a week

Get a working Apps Script snippet in your inbox, weekly.