0

I want to attach files to CouchbaseLite document. How can I do so? I did not find any code sample on official CBLite website for this - CBLite code Sample. I am still stuck how to accomplish it.

One way to do this in code is:

Document document = mDatabaseLocal.createDocument();
    document.getCurrentRevision().createRevision().setAttachment(name, contentType, contentStream);

But this is not clear. *What should be the name?* - It is the absolute path of the attachment on your local disk?

For contentType: I do not know if there exists any enum class or constants that I can pass as contentType.

How would I attach multiple files to a document? Do I need to create unsavedRevision for every attachment?

Master
  • 2,945
  • 5
  • 34
  • 65

2 Answers2

1

The name must be unique per attachment, and doesn't refer to the local file, it refers to the name that you want to fetch it from on the document.

In this case you would call createRevision() once and then setAttachment() multiple times on the revision, before saving it.

J Chris A
  • 1,014
  • 8
  • 12
0

you have to put an inputstream as attachment to your document.

A example can be found here CouchBase Attachment Example.

You have to convert each file into an InputStream and then you can set it to the document.

For convert you can use something like this:

private InputStream getAsStream(YourData data)
{
    baos = new ByteArrayOutputStream();
    try
    {
        objOstream = new ObjectOutputStream(baos);
        objOstream.writeObject(data);
    } catch (IOException e)
    {
        e.printStackTrace();
    }
    bArray = baos.toByteArray();
    bais = new ByteArrayInputStream(bArray);
    return bais;
}

In this example YourData can be every object or some of your own objectTypes. Hope this explanation will help you.

Mike
  • 421
  • 4
  • 10