RavenDB附件 - 功能怎么办?附件、功能、RavenDB

2023-09-03 12:57:38 作者:也许べ已没有也许

我有一个文件输入控制。

I have a file input control.

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

可以说,我浏览到 C:/Instruction.pdf 文件并点击提交。在提交,我要保存在RavenDB的文件,并在以后检索和下载该目的。我看到这个链接http://ravendb.net/docs/client-api/attachments 上面写着..做到这一点。

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

我不继1,2,3什么意思在这里意味着什么要说的视频/ 2,在命令我怎么可以用这两行使用它在我的情况..保存字/ PDF文件在ravendb ..如果任何一个曾做过这样的事,请告知。

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.

我不明白一件事..如何附件保存。如果我想存储附件本身(比如PDF)它是独立存储在ravendb ..我只是存储,它与相关的附件在主文档中的关键?如果是这样的话,哪里是物理存储在ravendb的PDF?我看可以吗?

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?

推荐答案

在1,2,3只是示例数据。它是什么,试图越过的是,你创建一个内存流无论你想然后使用内存流中的Put​​Attachment方法。下面是特设的,而不是测试,但应该工作:

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

编辑为的问题,其余

如何连接存储?我相信这是一个物业控股附件的字节数组的JSON文件 是文档独立存储?是。附件是未收录的专门文件,但它是数据库的一部分,因此,像复制工作任务。 在我应该存储附件的关键主文档,它与相关的吗?是的,你将引用的主要随时随地你想,你会只问乌鸦的附件与ID。 是PDF物理存储在ravendb?是。 您看见了吗?号它甚至出现在录音室(至少据我所知) How is attachment stored? I believe it is a json document with one property holding the byte array of the attachment 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. "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. Is the pdf stored physically in ravendb? Yes. Can you see it? No. It does even show up in the studio (at least as far as I know)

修改更正和更新的样本

        [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();
        }
    }