8

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?

ZVenue
  • 4,967
  • 16
  • 61
  • 92

2 Answers2

11

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();
        }
    }
Shawn C.
  • 6,446
  • 3
  • 34
  • 38
  • So how would you upload a pdf for example.. like in my OP. – ZVenue Jul 12 '12 at 19:05
  • If it does not show up in the studio.. how do I know its there – ZVenue Jul 12 '12 at 19:24
  • Thanks for the edited response. My question from the code.. I dont see how I can get the pdf file I pick from html file control into RavenDB. where am I passing that .. I dont see that. – ZVenue Jul 12 '12 at 19:28
  • In the Corrected Sample the top function is the Upload which you would post to from your, in this case asp.net mvc, form. byte[] bytes = ReadToEnd(file.InputStream) converts the pdf inputstream to a byte array which is then used by the memorystream. The memorystream then is used in the putattachment – Shawn C. Jul 12 '12 at 19:46
  • Shawn C - thank you for the updated post. Two things..1. Put attachment is taking a byte[] as third argument, so I removed the using memory stream line of your code.. using the memory stream is giving me error 'cannot convert byte[] to memorystream. 2. What is content(id) – ZVenue Jul 16 '12 at 15:16
  • (HttpPostedFileBase file) in Upload method is causing me issues. Is 'file' parameter not a string... that I am passing.. the string that is path of the file that is picked from the "File HTML Control" ? thats how I have it set up... how do I do this in this case. – ZVenue Jul 16 '12 at 18:12
  • If you are just passing a string to the method instead of a HttpPostedFile and that string is a path to a file then you would just change "byte[] bytes = ReadToEnd(file.InputStream);" to "byte[] bytes= System.IO.File.ReadAllBytes(PathToYourFile);" – Shawn C. Jul 16 '12 at 20:16
  • Thank you, that works....DateTime.Now.Second.ToString(CultureInfo.InvariantCulture); Does this give a unique value every time I want to attach a document. What is the purpose of using that for id value? – ZVenue Jul 18 '12 at 15:36
  • 1
    It is unique up to sixty times. I would not recommended at all but needed something for a quick example purposes. Since raven doesn't provided a id I would recommend using something that is unique like a guid or just a random string so that you don't overwrite one attachment with another. Something like http://stackoverflow.com/a/730418/1264360 should work well. – Shawn C. Jul 18 '12 at 17:18
8
  • 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

Ayende Rahien
  • 22,925
  • 1
  • 36
  • 41
  • If it's a private document, do I want to use attachment? Will other people be able to access it? – asunrey Feb 28 '13 at 02:33