RavenDB附件 - 功能怎么办?

ZVe*_*nue 8 .net c# nosql ravendb

我有一个文件输入控件.

   <input type="file" name="file" id="SaveFileToDB"/>
Run Code Online (Sandbox Code Playgroud)

让我们说我浏览到C:/Instruction.pdf文档并单击提交.在提交时,我想将文档保存在RavenDB中,稍后将其检索以供下载.我看到这个链接http://ravendb.net/docs/client-api/attachments 说...做这个..

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

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

我不遵循1,2,3这里的意思以及在命令中说视频/ 2意味着什么...我如何在我的情况下使用这两行来保存word/pdf在ravendb中.如果有人以前做过这样的事情,请告知.

我不清楚一件事......附件是如何存储的.如果我想存储附件本身(比如pdf),它会独立存储在ravendb ..我只是将附件的密钥存储在与之关联的主文档中?如果是这样的话,pdf在物理上存储在ravendb中的哪个位置?我可以看吗?

Sha*_* C. 11

1,2,3只是示例数据.它试图获得的是你创建一个你想要的内存流然后在PutAttachment方法中使用该内存流.以下是临时的,未经测试但应该有效:

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

编辑了其余的问题

  1. 如何存储附件?我相信它是一个json文档,其中一个属性包含附件的字节数组
  2. "文件"是否独立存储?是.附件是一个未编制索引的特殊文档,但它是数据库的一部分,因此复制等任务可以正常工作.
  3. "我应该"将附件的密钥存储在与其关联的主文档中吗?是的,您会引用密钥,并且只要您想要获得该密钥,您就可以向Raven询问具有该ID的附件.
  4. pdf是否实际存储在ravendb中?是.
  5. 你能看见它吗?不,它甚至出现在工作室里(至少据我所知)

编辑已更正和更新的示例

        [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();
        }
    }
Run Code Online (Sandbox Code Playgroud)


Aye*_*ien 8

  • 如何存储附件?

它作为二进制数据存储在RavenDB中.它不存储为json.

  • "文件"是否独立存储?

这里没有文档,您有一些与附件关联的元数据,它不是一个seaprate文档.

  • "我应该"将附件的密钥存储在与其关联的主文档中吗?

是的,没有办法查询.

  • pdf是否实际存储在ravendb中?

  • 你能看见它吗?

只有当你直接去附件时,例如 http://localhost:8080/static/ATTACHMENT_KEY

它不会在UI中显示