【问题标题】:RavenDB Attachments - Functionality how to do?RavenDB 附件 - 功能怎么办?
【发布时间】:2012-07-12 17:42:20
【问题描述】:

我有一个文件输入控件。

   <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 在这里的含义以及在命令中说视频/2 的含义...我如何使用这两行来使用它.. 保存 word/pdf在ravendb ..如果有人以前做过这样的事情,请告知。

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

【问题讨论】:

    标签: c# .net nosql ravendb


    【解决方案1】:

    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. “我应该”将附件的密钥存储在与之关联的主文档中吗?是的,您会引用密钥,并且任何时候您想获得它,您只需向 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();
            }
        }
    

    【讨论】:

    • 那么,例如,您将如何上传 pdf.. 就像在我的 OP 中一样。
    • 如果它没有出现在工作室..我怎么知道它在那里
    • 感谢编辑回复。我的代码中的问题.. 我看不到如何将我从 html 文件控件中选择的 pdf 文件获取到 RavenDB 中。我在哪里传递..我没有看到。
    • 在更正后的示例中,最重要的功能是上传,您将从您的表单(在本例中为 asp.net mvc)发布到该表单。 byte[] bytes = ReadToEnd(file.InputStream) 将 pdf 输入流转换为字节数组,然后由内存流使用。然后在 putattachment 中使用内存流
    • 它是唯一的多达六十次。我根本不会推荐,但需要一些东西来快速举例。由于 raven 没有提供 id,我建议使用独特的东西,比如 guid 或只是一个随机字符串,这样你就不会用另一个附件覆盖一个附件。 stackoverflow.com/a/730418/1264360 之类的东西应该可以正常工作。
    【解决方案2】:
    • 附件是如何存储的?

    它在 RavenDB 中以二进制数据的形式存储。它不存储为 json。

    • “文档”是否独立存储?

    这里没有文档,您有一些与附件相关联的元数据,它不是单独的文档。

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

    是的,没有办法查询。

    • pdf 是否物理存储在 ravendb 中?

    是的

    • 你能看到吗?

    仅当您直接进入附件时,例如http://localhost:8080/static/ATTACHMENT_KEY

    它不会显示在 UI 中

    【讨论】:

    • 如果是私人文档,我要使用附件吗?其他人可以访问它吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-11-03
    • 2014-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-20
    相关资源
    最近更新 更多