2012-07-12 34 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