Skip to content

Commit

Permalink
RDoc-728 Attachment Put method
Browse files Browse the repository at this point in the history
  • Loading branch information
Fitzchak Yitzchaki committed Dec 5, 2017
1 parent 8029c01 commit e4f51ac
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@
<None Include="client-api\session\attachments\.docs.json" />
<None Include="client-api\session\attachments\how-to\.docs.json" />
<None Include="client-api\session\attachments\how-to\get-attachment-metadata-only.dotnet.markdown" />
<None Include="client-api\session\attachments\how-to\update-attachment-metadata-only.dotnet.markdown" />
<None Include="client-api\session\attachments\put.dotnet.markdown" />
<None Include="client-api\session\attachments\delete.dotnet.markdown" />
<None Include="client-api\session\attachments\get.dotnet.markdown" />
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
# Attachments : Put

**PutAttachment** is used to insert or update an attachment in a database.
In order to put an attachment in RavenDB you need to create a document and than you can attach an attachment to the document using the `Advanced.Attachments.Store` method.
Note that attachments are trnsactional and would be save when you call `session.SaveChanges`.

## Syntax

{CODE put_1@ClientApi\Commands\Attachments\Put.cs /}
The can store an attachment using the folloinwg `session.Advanced.Attachments.Store` methods:

| Parameters | | |
| ------------- | ------------- | ----- |
| **key** | string | unique key under which attachment will be stored |
| **etag** | Etag | current attachment etag, used for concurrency checks (`null` to skip check) |
| **data** | Stream | attachment data |
| **metadata** | RavenJObject | attachment metadata |
{CODE StoreSyntax@ClientApi\Session\Attachments.cs /}

## Example

{CODE put_2@ClientApi\Commands\Attachments\Put.cs /}
{CODE StoreAttachment@ClientApi\Session\Attachments.cs /}

{CODE StoreAttachmentAsync@ClientApi\Session\Attachments.cs /}

## Related articles

- [How to **update** attachment **metadata** only?](../../../client-api/commands/attachments/how-to/update-attachment-metadata-only)
- [GetAttachment](../../../client-api/commands/attachments/get)
- [DeleteAttachment](../../../client-api/commands/attachments/delete
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ In order to store album of pictures in RavenDB 4.0 you can create the folloinwg

{
"UserId": "users/1",
"Name": "Holidays Travel",
"Name": "Holidays",
"Description": "Holidays travel pictures of the all family",
"Tags": ["Holydays Travel", "All Family"],
"Tags": ["Holidays Travel", "All Family"],
"@metadata": {
"@collection": "Albums"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ public class Album
public string Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string[] Tags { get; set; }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,70 @@ public class Attachments
{
private interface IFoo
{
#region put_1
void StoreAttachment(string documentId, string name, Stream stream, string contentType = null);
void StoreAttachment(object entity, string name, Stream stream, string contentType = null);
#region StoreSyntax
void Store(string documentId, string name, Stream stream, string contentType = null);
void Store(object entity, string name, Stream stream, string contentType = null);
#endregion

#region delete_1
void DeleteAttachment(string documentId, string name);
void DeleteAttachment(string documentId, string name);
void DeleteAttachment(object entity, string name);
#endregion
}

public void StoreAttachment()
{
using (var store = new DocumentStore())
{
#region StoreAttachment
using (var session = store.OpenSession())
using (var file1 = File.Open("001.jpg", FileMode.Open))
using (var file2 = File.Open("002.jpg", FileMode.Open))
using (var file3 = File.Open("003.jpg", FileMode.Open))
using (var file4 = File.Open("004.mp4", FileMode.Open))
{
var album = new Album
{
Name = "Holidays",
Description = "Holidays travel pictures of the all family",
Tags = new[] {"Holidays Travel", "All Family"},
};
session.Store(album, "albums/1");

session.Advanced.Attachments.Store("albums/1", "001.jpg", file1, "image/jpeg");
session.Advanced.Attachments.Store("albums/1", "002.jpg", file2, "image/jpeg");
session.Advanced.Attachments.Store("albums/1", "003.jpg", file3, "image/jpeg");
session.Advanced.Attachments.Store("albums/1", "004.mp4", file4, "video/mp4");

session.SaveChanges();
}
#endregion
}
}

public async Task StoreAttachmentAsync()
{
using (var store = new DocumentStore())
{
#region StoreAttachmentAsync
using (var session = store.OpenAsyncSession())
using (var file = File.Open("sea.png", FileMode.Open))
using (var file1 = File.Open("001.jpg", FileMode.Open))
using (var file2 = File.Open("002.jpg", FileMode.Open))
using (var file3 = File.Open("003.jpg", FileMode.Open))
using (var file4 = File.Open("004.mp4", FileMode.Open))
{
var album = new Album
{
Name = "Holidays",
Description = "Holidays 2014"
Description = "Holidays travel pictures of the all family",
Tags = new[] {"Holidays Travel", "All Family"},
};
await session.StoreAsync(album, "albums/1");

session.Advanced.Attachments.Store("albums/1", "sea.png", file, "image/png");
session.Advanced.Attachments.Store("albums/1", "001.jpg", file1, "image/jpeg");
session.Advanced.Attachments.Store("albums/1", "002.jpg", file2, "image/jpeg");
session.Advanced.Attachments.Store("albums/1", "003.jpg", file3, "image/jpeg");
session.Advanced.Attachments.Store("albums/1", "004.mp4", file4, "video/mp4");

await session.SaveChangesAsync();
}
Expand Down

This file was deleted.

0 comments on commit e4f51ac

Please sign in to comment.