c# - RavenDB 附件 - 功能怎么做?

标签 c# .net nosql ravendb

我有一个文件输入控件。

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

假设我浏览到 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"}});

我没有遵循 1、2、3 在这里的意思以及在命令中说 videos/2 的意思……我如何使用这两行来在我的情况下使用它……来保存 word/pdf在ravendb ..如果有人以前做过这样的事情,请告知。

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

最佳答案

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" }
                                                              });
        }

对其余问题进行了编辑

  1. 附件是如何存储的?我相信这是一个 json 文档,其中一个属性保存附件的字节数组
  2. “文档”是否独立存储?是的。附件是一种没有索引的特殊文档,但它是数据库的一部分,因此可以执行复制等任务。
  3. “我应该”将附件的 key 存储在与之关联的主文档中吗?是的,您会引用 key ,任何时候您想要获取 key ,您只需向 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();
        }
    }

关于c# - RavenDB 附件 - 功能怎么做?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11457533/

相关文章:

c# - 为什么子类不能使用基类保护的构造函数创建新对象?

c# - Entity Framework 核心 : LINQ advise needed on better approach using include for relational tables

c# - List<Class> 到 List<string>

c# - 基本类型和继承类型的工作通用列表

c# - 无法分配静态双指针变量

rdbms - 为什么 RDBMS 被认为适用于 CAP 定理 (CA)

cassandra - Cassandra 中的宽行与集合

c# - 我可以将继承与扩展方法一起使用吗?

c# - 将较小的数组插入较大的数组,反之亦然

database-design - 分区加权有向图(基于键/值数据库)