Copying Images and Other Binaries

Using the JavaScript API and jQuery's save and content methods, you can copy the values of binary attributes. The following code copies the value of the binary attribute whose DOM-ID is source_binary_field to the binary attribute named target_binary_field:

Copy
var binary_content = $("source_binary_field").scrivito("content");
$("#target_binary_field").scrivito("save", binary_content);

You can also have an existing binary copied to a new page while creating it:

Copy
var binary_content = $("source_binary_field").scrivito("content");
scrivito.create_obj(_obj_class: 'Page', target_binary: binary_content);

It is also possible to change the file name and the content type of the copied binary using the copy function of the binary content and providing the filename or content_type option, or both:

Copy
var original_binary_content = $("source_binary_field").scrivito("content");
original_binary_content.filename     //> "cat.png"
original_binary_content.content_type //> "application/octet-stream"

var copied_binary_content = original_binary_content.copy({
  filename: "dog.png",      // Change the file name.
  content_type: "image/png" // Fix the content type.
});

$("#target_binary_field").scrivito("save", copied_binary_content.);