see on GitHub

Registering an upload

In order to register a file upload within the session, you can use one of four overloads of the RegisterUpload method.

Syntax

First two overloads allow you to register an upload by specifying the file's full path and content. The content can be represented either by the stream object or the write action that is allowed to the directly write to the HTTP request stream:

void RegisterUpload(string path, Stream stream, RavenJObject metadata = null, Etag etag = null);
void RegisterUpload(string path, long fileSize, Action<Stream> write, RavenJObject metadata = null, Etag etag = null);

Next two overloads accept the FileHeader object, which represents the file in the session, instead of the file's full path.

void RegisterUpload(FileHeader file, Stream stream, Etag etag = null);
void RegisterUpload(FileHeader file, long fileSize, Action<Stream> write, Etag etag = null);

Parameters
path string The full path of the file
file FileHeader The file represented by the FileHeader
stream Stream The file content that will be copied to the HTTP request
fileSize long The declared number of bytes to write in write action
write Action<Stream> The action which writes file content bytes directly to the HTTP request stream
metadata RavenJObject The file's metadata
etag Etag Current file Etag, used for concurrency checks (null will skip the check)

Example I

The below code will upload an entire file stored on a local disk to RavenFS:

using (Stream content = File.OpenRead(@"C:\intro.avi"))
{
	session.RegisterUpload("/movies/intro.avi", content);

	await session.SaveChangesAsync();
}

File content

The actual upload is made when the SaveChangesAsync is run. The stream, which is file content, needs to be available to read at that moment. The stream value must not be disposed before the save changes call.

Example II

You can also generate the file content dynamically:

session.RegisterUpload("random.bin", 128, stream =>
{
	var bytes = new byte[128];
	new Random().NextBytes(bytes);

	stream.Write(bytes, 0, 128);
});

await session.SaveChangesAsync();

Possible BadRequestException

If the write action puts fewer bytes than declared, RavenFS will detect this and cancel the upload by throwing the BadRequestException when the SaveChangesAsync is called.

Example III

This example uploads a file only if it doesn't exits in RavenFS or if a local file was modified at least one hour later than the remote one:

FileHeader ravenFile = await session.LoadFileAsync("/movies/intro.avi");

string localFile = @"C:\intro.avi";

if (ravenFile == null || new FileInfo(localFile).LastWriteTime - ravenFile.LastModified > TimeSpan.FromHours(1))
{
	using (Stream content = File.OpenRead(localFile))
	{
		session.RegisterUpload(ravenFile, content);

		await session.SaveChangesAsync();
	}
}