2012-07-12 28 views
8

我有一个文件输入控件。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这里意味着什么要说的视频/ 2的命令......我怎么可以使用这两行来使用它在我的情况..要保存word/pdf在ravendb ..如果任何人之前做过这样的事情,请告知。

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

回答

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

编辑的的问题

  1. 其余存储附件如何?我相信这是一个JSON文档,其中一个属性保存着附件的字节数组。
  2. “文档”是独立存储的吗?是。附件是一种特殊的文档,它没有被索引,但是它是数据库的一部分,因此像复制这样的任务可以工作。
  3. “我应该”将附件的关键字存储在与其关联的主文档中吗?是的,你会参考钥匙,并且任何时候你想得到的,你会问雷文与该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(); 
     } 
    } 
+0

那么你将如何上传例如PDF ..喜欢在我的任择议定书。 – ZVenue 2012-07-12 19:05:16

+0

如果它没有出现在工作室中..我怎么知道它有 – ZVenue 2012-07-12 19:24:19

+0

感谢编辑的回应。我从代码的问题..我没有看到我怎么能得到我从HTML文件控制选择到RavenDB的PDF文件。我在哪里通过..我没有看到。 – ZVenue 2012-07-12 19:28:53

8
  • 如何保存附件?

它存储为内部RavenDB二进制数据。它不被存储为json。

  • 是 “文档” 独立存储?

这里没有文档,你有一些与附件关联的元数据,它不是一个安全的文档。

  • “我应该”将附件的关键字存储在与其关联的主文档中吗?

是的,没有办法查询。

  • pdf是否物理存储在ravendb?

  • 你能看到吗?

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

它不会显示在用户界面

+0

如果是私人文件,我想使用附件吗?其他人能够访问它吗? – asunrey 2013-02-28 02:33:25