Binary.upload(source, options)

Prepares binary data locally for uploading to Scrivito.

Copy
futureBinary = Scrivito.Binary.upload(file)

By default, the type property of the passed-in source is used as the content type but can be overridden using options. If source is a Blob, be sure to specify a filename in the options. For File objects, the filename falls back to "File#file".

Note: This method does not perform the actual upload but returns a FutureBinary. The actual upload can be triggered by putting the FutureBinary into an Obj.

Params

  • source (Blob or File) - Local data that can be uploaded to a Binary
  • options (optional) (Object) - Options to pass when uploading a Binary
    • filename (String) - The file name to use for the new Binary; mandatory for blobs.
    • contentType (String) - A content type for overriding the type of the source.

Throws

  • ArgumentError - If the source is neither a Blob nor a File.
  • ArgumentError - If any of the passed-in options are blank.
  • ArgumentError - If source is a "Blob" and no filename is given.

Returns

FutureBinary

Examples

Running the following code in the console creates an image object from SVG markup. You should be able to see this image in the Content Browser.

Copy
svg = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 50 50"><circle cx="25" cy="25" r="25" fill="green"/></svg>'
blob = new Blob([svg], {type: "image/svg+xml"})
image = Scrivito.getClass('Image').create({ })
futureBinary = Scrivito.Binary.upload(blob, { filename: "my.svg" })
futureBinary.into(image).then(binary => image.update({ blob: binary }))

The following example uploads an image file a user has selected using the file input element whose ID is “upload”. The uploaded file is put into an new image object:

Copy
chosenFile = document.getElementById('upload').files[0]
myObj = Image.create({ })
futureBinary = Scrivito.Binary.upload(chosenFile)
futureBinary.into(myObj).then(binary => myObj.update({ blob: binary }))

In line three of the example above, the file is uploaded with a filename and content type determined by the browser. You can specify a custom filename and content type when uploading:

Copy
futureBinary = Scrivito.Binary.upload(chosenFile, {
  contentType: 'image/jpeg',
  filename: 'my_cat.jpg'
})