Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# RavenDB附件-功能如何实现?_C#_.net_Nosql_Ravendb - Fatal编程技术网

C# RavenDB附件-功能如何实现?

C# RavenDB附件-功能如何实现?,c#,.net,nosql,ravendb,C#,.net,Nosql,Ravendb,我有一个文件输入控件 <input type="file" name="file" id="SaveFileToDB"/> 我不明白1,2,3在这里的意思,也不明白在命令中说videos/2意味着什么。。。我怎么能用这两行在我的案例中使用它。。要在ravendb中保存word/PDF。。如果有人以前做过这样的事情,请告知 有一件事我不清楚。。附件的存储方式。如果我想存储附件本身(比如pdf),它将独立存储在ravendb中。。我只是将附件的密钥存储在它关联的主文档中?如果是这

我有一个文件输入控件

   <input type="file" name="file" id="SaveFileToDB"/>
我不明白1,2,3在这里的意思,也不明白在命令中说videos/2意味着什么。。。我怎么能用这两行在我的案例中使用它。。要在ravendb中保存word/PDF。。如果有人以前做过这样的事情,请告知


有一件事我不清楚。。附件的存储方式。如果我想存储附件本身(比如pdf),它将独立存储在ravendb中。。我只是将附件的密钥存储在它关联的主文档中?如果是这样,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" }
                                                              });
        }
对其余问题进行编辑

  • 附件是如何存储的?我相信这是一个json文档,其中一个属性包含附件的字节数组
  • “文件”是否独立存储?对附件是一种特殊的文档,它没有编制索引,但它是数据库的一部分,以便执行复制等任务
  • “我应该”将附件的密钥存储在与其关联的主文档中吗?是的,你会参考钥匙,任何时候你想要得到钥匙,你只需要向瑞文索要带有该id的附件
  • pdf是否物理存储在ravendb中?对
  • 你能看见吗?没有。它甚至出现在演播室里(至少据我所知)
  • 编辑已更正和更新的样本

            [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();
            }
        }
    
    • 附件是如何存储的
    它作为二进制数据存储在RavenDB中。它不存储为json

    • “文件”是否独立存储
    这里没有文档,您有一些与附件关联的元数据,它不是seaprate文档

    • “我应该”将附件的密钥存储在与其关联的主文档中吗
    是的,没有办法对此提出质疑

    • pdf是否物理存储在ravendb中

    • 你能看见吗
    仅当您直接转到附件时,如
    http://localhost:8080/static/ATTACHMENT_KEY


    它不会显示在UI中,因此您将如何上传pdf,例如。。就像在我的作品里。如果它没有出现在演播室里。。我怎么知道呢?谢谢编辑后的回复。我的问题来自代码。。我不知道如何将从html文件控件中选择的pdf文件放入RavenDB。我在哪里经过那。。我看不出来。在更正后的示例中,最上面的函数是您将从asp.net mvc表单发布到的上载。byte[]bytes=ReadToEnd(file.InputStream)将pdf InputStream转换为字节数组,然后由memorystream使用。然后在putattachmentIt中使用memorystream,它的唯一性高达60倍。我一点也不推荐,但需要一些简单的例子。由于raven没有提供id,我建议您使用唯一的guid或随机字符串,这样您就不会用另一个附件覆盖一个附件。类似的东西应该很好用。如果是私人文档,我想使用附件吗?其他人可以访问它吗?
            [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();
            }
        }