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" }
});
}
对其余问题进行编辑
[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是否物理存储在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();
}
}