Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RavenDB Attachments - Functionality how to do?

I have a file input control.

   <input type="file" name="file" id="SaveFileToDB"/>

Lets say I browse to C:/Instruction.pdf document and click on submit. On Submit, I want to save the document in RavenDB and also later retrieve it for download purposes. I saw this link http://ravendb.net/docs/client-api/attachments that says.. do this..

Stream data = new MemoryStream(new byte[] { 1, 2, 3 }); 

documentStore.DatabaseCommands.PutAttachment("videos/2", null, data,
  new RavenJObject {{"Description", "Kids play in the garden"}});

I am not following what 1,2,3 mean here and what it means to say videos/2 in the command... how I can use these two lines to use it in my case.. to save word/pdfs in ravendb.. if any one has done such thing before, please advise.

I am not clear on one thing.. how the attachment is stored. If I want to store the attachment itself (say pdf) it is stored independently in ravendb.. and I just store the key of the attachment in the main document that it is associated with? If that is so, where is the pdf stored physically in ravendb? can I see it?

like image 706
ZVenue Avatar asked Jul 12 '12 17:07

ZVenue


2 Answers

The 1,2,3 is just example data. What it is trying to get across is that you create a memory stream of whatever you want then use that memory stream in the PutAttachment method. Below is ad-hoc and not tested but should work:

        using (var mem = new MemoryStream(file.InputStream)
        {
            _documentStore.DatabaseCommands.PutAttachment("upload/" + YourUID, null, mem,
                                                          new RavenJObject
                                                              {
                                                                  { "OtherData", "Can Go here" }, 
                                                                  { "MoreData", "Here" }
                                                              });
        }

Edited for the rest of the questions

  1. How is attachment stored? I believe it is a json document with one property holding the byte array of the attachment
  2. Is the "document" stored independently? Yes. An attachment is a special document that is not indexed but it is part of the database so that tasks like replication work.
  3. "Should I" store the key of the attachment in the main document that it is associated with? Yes you would reference the Key and anytime you want to get that you would just ask Raven for the attachment with that id.
  4. Is the pdf stored physically in ravendb? Yes.
  5. Can you see it? No. It does even show up in the studio (at least as far as I know)

Edit Corrected and Updated Sample

        [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Upload(HttpPostedFileBase file)
    {
        byte[] bytes = ReadToEnd(file.InputStream);
        var id = "upload/" + DateTime.Now.Second.ToString(CultureInfo.InvariantCulture);
        using (var mem = new MemoryStream(bytes))
        {
            DocumentStore.DatabaseCommands.PutAttachment(id, null, mem,
                                                          new RavenJObject
                                                          {
                                                              {"OtherData", "Can Go here"},
                                                              {"MoreData", "Here"},
                                                              {"ContentType", file.ContentType}
                                                          });
        }

        return Content(id);
    }

    public FileContentResult GetFile(string id)
    {
        var attachment = DocumentStore.DatabaseCommands.GetAttachment("upload/" + id);
        return new FileContentResult(ReadFully(attachment.Data()), attachment.Metadata["ContentType"].ToString());
    }

    public static byte[] ReadToEnd(Stream stream)
    {
        long originalPosition = 0;

        if (stream.CanSeek)
        {
            originalPosition = stream.Position;
            stream.Position = 0;
        }

        try
        {
            var readBuffer = new byte[4096];

            int totalBytesRead = 0;
            int bytesRead;

            while ((bytesRead = stream.Read(readBuffer, totalBytesRead, readBuffer.Length - totalBytesRead)) > 0)
            {
                totalBytesRead += bytesRead;

                if (totalBytesRead == readBuffer.Length)
                {
                    int nextByte = stream.ReadByte();
                    if (nextByte != -1)
                    {
                        var temp = new byte[readBuffer.Length*2];
                        Buffer.BlockCopy(readBuffer, 0, temp, 0, readBuffer.Length);
                        Buffer.SetByte(temp, totalBytesRead, (byte) nextByte);
                        readBuffer = temp;
                        totalBytesRead++;
                    }
                }
            }

            byte[] buffer = readBuffer;
            if (readBuffer.Length != totalBytesRead)
            {
                buffer = new byte[totalBytesRead];
                Buffer.BlockCopy(readBuffer, 0, buffer, 0, totalBytesRead);
            }
            return buffer;
        }
        finally
        {
            if (stream.CanSeek)
            {
                stream.Position = originalPosition;
            }
        }
    }

    public static byte[] ReadFully(Stream input)
    {
        byte[] buffer = new byte[16 * 1024];
        using (MemoryStream ms = new MemoryStream())
        {
            int read;
            while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
            {
                ms.Write(buffer, 0, read);
            }
            return ms.ToArray();
        }
    }
like image 113
Shawn C. Avatar answered Oct 19 '22 00:10

Shawn C.


  • How is attachment stored?

It is stored as binary data inside RavenDB. It is NOT stored as json.

  • Is the "document" stored independently?

There isn't a document here, you have some metadata that is associated with the attachment, it isn't a seaprate document.

  • "Should I" store the key of the attachment in the main document that it is associated with?

Yes, there is no way to query for that.

  • Is the pdf stored physically in ravendb?

Yes

  • Can you see it?

Only if you go to the attachment directly, such as http://localhost:8080/static/ATTACHMENT_KEY

It won't show in the UI

like image 42
Ayende Rahien Avatar answered Oct 18 '22 22:10

Ayende Rahien