- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有一个文件输入控件。
<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 在这里的意思以及在命令中说 videos/2 的意思……我如何使用这两行来在我的情况下使用它……来保存 word/pdf在ravendb ..如果有人以前做过这样的事情,请告知。
我不清楚一件事.. 附件是如何存储的。如果我想存储附件本身(比如 pdf),它会独立存储在 ravendb 中。我只是将附件的 key 存储在与其关联的主文档中?如果是这样,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();
}
}
关于c# - RavenDB 附件 - 功能怎么做?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11457533/
我正在玩Raven DB几天,我想将其用作Web聊天应用程序的存储。我有包含一些用户数据和聊天记录的文档-这是大集合的聊天消息。 每次加载用户文档时,聊天记录也会加载,即使我只需要几个字段,例如:用户
我的实体是: class Resource { string Name; string EmployeeId; } 如何查询多名员工的资源?我尝试了这个: Resource[] Fin
我知道这篇文章的标题会让一些人感到畏缩。我自 2011 年 12 月以来一直在使用 RavenDB,并且逐渐意识到我以不适合使用文档数据库的方式对数据进行建模。首先,我完全明白这一点。其次,考虑到我目
考虑 2 个查询: var test1 = store.OpenSession().Query().Where(x => x.Id == 1).ToList(); var test2 =
我正在运行 RavenDB.Client 2.0.2173-Unstable。我正在创建一个 Multi-Tenancy 系统,作为我注册过程的一部分,我想创建一个新的 Raven 数据库。 我有三行
我正在使用在 Visual Studio 2010 中通过 NuGet 安装的嵌入式 RavenDB => RavenDB-Embedded.1.0.499 包。它正在我阅读完这篇优秀文章后开始的当前
我们在 AWS 上有一个带有集群 RavenDB 服务器 (5.23) 的 DotNet Core API (C#)。创建文档时,Id 具有不同的格式,具体取决于客户端。如果使用 Swagger/Po
我想知道取回搜索记录总数的最佳方法是什么,同时取回第 N 个 128 记录块数据段,这似乎是 RavenDb 运行时强加的上限。 例如给定这个查询,我还需要知道记录的总数。 var bookmarks
我使用的是 RavenDB 2.0.3.0 版。连接调试器后,涉及 RavenDB 的所有内容都运行得非常缓慢。每个查询需要几秒钟才能完成。 如果我在调试器坐在那里时按下暂停键,我总是看到它挂断了以下
我使用以下代码从 RavenDB 中删除文档。在 Raven UI 的底部,我可以看到文档数量从 3,000,000 减少到 2,000,000。但是“数据”文件的大小不会缩小。它总是大约100G。
我想在我的 Raven DB 中轮询新文档。推荐的方法是什么?我可以使用 IndexTimestamp 还是可以依赖文档的顺序? 我想我想分两步完成: 1.检查是否有新的东西,如果有: 1.1。获取最
我已经更改了我的数据模型,并且需要将所有对象的属性更改为 null,因为在我的模型更改后反序列化它们会引发异常。 我猜我可以为每个文档发布一个补丁,但我找不到任何方法来做到这一点,修补某种类型的每个文
有没有办法在进行空间搜索时从 RavenDB 获取距离计算值。我尝试转换建议的 _ = SpatialIndex.Generate(r.Latitude, r.Longitude) 至 距离 = Sp
是否有一种明智的方法来 stub /模拟调用 IDocumentSession.Query() 的结果? ? 我有一个命令,我想验证在对象上调用了方法(即正在测试的“单元”是命令,而不是命令编排的对象
我想在存储在 RavenDB 文档数据库中的两个实体之间有一个引用。由于这不是关系数据库,我知道我应该使用 RavenDBs 文档中描述的非规范化引用技术。虽然起初这看起来不错,但一旦我开始创建一个真
是否可以使用返回单个文档但不是关键的属性从数据存储加载(而不是查询)文档? 在我的场景中,我有用户和品牌。品牌是用户,但用户不一定是品牌。每个品牌都有一个 UserId 属性及其 Id。有没有办法通过
通过官方 RavenDB 阅读一个简单的问题 documentation ,我知道您可以使用客户端 API 函数执行这些操作,但不能使用 RQL,或者无论如何都可以使用 RQL 执行。 最佳答案 您正
我正尝试在 RavenDB 中检索我按 int 列表排序的文档。 Queryable = Queryable.OrderBy(dto => SearchIds.IndexOf(dto.Property
我刚刚让 RavenDB 开始工作。 我意识到我必须启动 RavenDB\packages\RavenDB.1.0.573\server.exe 才能通过以下方式在我的应用程序中访问它: Do
RavenDB 是否有众所周知的做法来检查 RavenDb 的可用性或检查它在任何给定时间的过载程度?我研究了一些 ravendb API 服务。好像没找到关于健康监测的。有没有像官方的 ravend
我是一名优秀的程序员,十分优秀!