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();
}
}
那麼你將如何上傳例如PDF ..喜歡在我的任擇議定書。 – ZVenue 2012-07-12 19:05:16
如果它沒有出現在工作室中..我怎麼知道它有 – ZVenue 2012-07-12 19:24:19
感謝編輯的迴應。我從代碼的問題..我沒有看到我怎麼能得到我從HTML文件控制選擇到RavenDB的PDF文件。我在哪裏通過..我沒有看到。 – ZVenue 2012-07-12 19:28:53