Skip to content
This repository has been archived by the owner on Mar 14, 2019. It is now read-only.

Insert One File From a Remote URL

Philipp edited this page Mar 1, 2015 · 1 revision

Insert One File From a Remote URL

In either client or server code:

Images.insert(url, function (error, fileObj) {
  //If !error, we have inserted new doc with ID fileObj._id, and
  //remote URL data will be downloaded and stored on the server. The
  //URL must support a HEAD request since we do one to get the 
  //content type, size, etc. for filtering inserts.
});

On the server, you can omit the callback and the method will block until the data download and insert are both complete. Then it will return the new FS.File instance.

var newFileObj = Images.insert(url);

On the client, you can omit the callback if you don't need to access the resulting FS.File instance, and any errors will be thrown.

When you pass a URL directly to insert, the filename will be extracted from the end of the URL string, but only if it ends with an extension. Otherwise filename will be null. If you want to avoid a null filename, you will have to explicitly attach the URL to a new FS.File instance and set the name:

var newFile = new FS.File();
newFile.attachData(url, function (error) {
  if (error) throw error;
  newFile.name("newImage.png");
  Images.insert(newFile, function (error, fileObj) {
    //If !error, we have inserted new doc with ID fileObj._id, and
    //remote URL data will be downloaded and stored on the server. The
    //URL must support a HEAD request since we do one to get the 
    //content type, size, etc. for filtering inserts.
  });
});