View Source

h2. As part of a Document

You can add attachments to a {{Document}}. Saving the document will add the attachment.

{code:java}
String name = "java-log.png";
Document d = new Document();
InputStream is = TestClass.class.getResourceAsStream("/fixtures/image.png");
d.addAttachment(name, is);
database.saveDocument(d);

Document d2 = database.fetchDocument(d.getId());
Attachment a = d2.getAttachment(name);
{code}


With an existing document:
{code:java}

Document d = database.fetchDocument(docId);

Attachment a = d.getAttachment("logo.png");

// Replace existing attachment
d.addAttachment("logo.png", new FileInputStream("logo2.png"));

// Add another attachment
d.addAttachment(new File("docs.tex")); // This will use the file name as the attachment name

ServerResponse response = database.saveDocument(d);
{code}

If this is an existing document and there already is an attachment with this name, the attachment will be replaced.