Skip to content

Commit 7b09da1

Browse files
committed
Improve file download documentation
1 parent e7d1be7 commit 7b09da1

File tree

1 file changed

+90
-65
lines changed
  • MFilesAPI.Extensions/Files/Downloading

1 file changed

+90
-65
lines changed

MFilesAPI.Extensions/Files/Downloading/Readme.md

Lines changed: 90 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -2,71 +2,100 @@
22

33
## FileDownloadLocation
44

5-
The `FileDownloadLocation.cs` class represents a physical folder on disk where temporary file downloads are stored. This class inherits from `IDisposable` and can automatically delete temporary files when the object is disposed:
5+
The `FileDownloadLocation.cs` class represents a physical folder on disk where temporary files are stored. The system temporary folder path is automatically used if no specific folder location is provided.
6+
7+
This class implements the `IDisposable` interface and can automatically delete the content of the folder it represent when the object is disposed, if the value of the `CleanDirectoryOnDisposal` property is `true`.
8+
9+
Usage example, using the system default temporary folder path, and the assembly name as folder name:
610

711
```csharp
8-
using(var downloadLocation = new FileDownloadLocation())
12+
using(FileDownloadLocation downloadLocation = new FileDownloadLocation())
913
{
14+
// Will use the system temporary folder path
15+
// and the running assembly name as folder name
16+
// since no specific folder has been provided in the constructor.
17+
1018
// Set the location to automatically clean on disposal.
11-
// Alternatively we could manually call downloadLocation.CleanTemporaryFiles().
19+
// This is true by default, so the following line is not needed
20+
// unless to set it to false.
1221
downloadLocation.CleanDirectoryOnDisposal = true;
22+
// Alternatively we could manually call downloadLocation.CleanTemporaryFiles().
1323
14-
// Perform file downloads.
15-
// It is assumed that objectFile and vault are already available/populated.
16-
using(var fileDownload = downloadLocation.DownloadFile(objectFile, vault))
17-
{
18-
// TODO: Perform operations on the downloaded file.
19-
}
24+
// TODO: Add, manipulate folders, files in that location,
25+
// they will be automatically deleted when this object is disposed
26+
// if CleanDirectoryOnDisposal is true,
27+
// or deleted when downloadLocation.CleanTemporaryFiles() is called.
2028
}
2129
```
2230

2331

32+
33+
To use a specific location, simply use the constructor overload that takes a path, and a folder name:
34+
35+
```csharp
36+
using (FileDownloadLocation downloadLocation = new FileDownloadLocation(@"C:\temp", "myfolder"))
37+
{
38+
// The folder represented by this FileDownloadLocation instance is:
39+
// C:\temp\myfolder
40+
}
41+
```
42+
43+
44+
2445
## TemporaryFileDownload
2546

26-
The `TemporaryFileDownload` class represents a download of a single file from the vault onto disk for temporary usage. Files can be downloaded with or without a `FileDownloadLocation` object, as appropriate:
47+
The `TemporaryFileDownload` class represents a download of a single file from the vault onto disk for temporary usage.
48+
49+
This class implements the `IDisposable` interface and automatically deletes the temporary file it represents when the object is disposed.
50+
51+
It cannot be directly instantiated using the `new` keyword.
2752

28-
### Using the static method
53+
Instances are either created using the `TemporaryFileDownload` class static method `Download`, or via the `DownloadFile` method of a `FileDownloadLocation` instance, or via extensions methods on `ObjectFile` instances.
54+
55+
### Using the `TemporaryFileDownload` static `Download` method
2956

3057
```csharp
3158
// Download the file.
3259
// It is assumed that objectFile and vault are already available/populated.
33-
var physicalFile = new FileInfo("C:\temp\mydownload.tmp");
34-
using(var fileDownload = FileDownload.DownloadFile(objectFile, vault, physicalFile))
60+
var physicalFile = new FileInfo(@"C:\temp\mydownload.tmp");
61+
using(TemporaryFileDownload fileDownload = TemporaryFileDownload.Download(objectFile, vault, physicalFile))
3562
{
36-
// TODO: Perform operations on physicalFile.
63+
// TODO: Perform operations on physical file.
3764
}
3865
```
3966

40-
### Using the instance method
67+
### Using a `FileDownloadLocation` instance `DownloadFile` method
4168

4269
```csharp
43-
// Initialise the file download object.
44-
// It is assumed that objectFile and vault are already available/populated.
45-
var physicalFile = new FileInfo(@"C:\temp\mydownload.tmp");
46-
using(var fileDownload = new FileDownload(objectFile, vault, physicalFile))
47-
{
48-
// Perform the download.
49-
fileDownload.DownloadFile();
70+
using(FileDownloadLocation downloadLocation = new FileDownloadLocation())
71+
{
72+
// Perform file downloads.
73+
// It is assumed that objectFile and vault are already available/populated.
74+
using(TemporaryFileDownload fileDownload = downloadLocation.DownloadFile(objectFile, vault))
75+
{
76+
// The downloaded file will be deleted when the TemporaryFileDownload instance is disposed.
77+
// TODO: Perform operations on the temporary downloaded file.
78+
}
5079

51-
// TODO: Perform operations on physicalFile.
80+
// Any other file or folder created in that folder will be deleted
81+
// when this FileDownloadLocation instance will be disposed.
5282
}
5383
```
5484

55-
### Using FileDownloadLocation
56-
57-
See example under [FileDownloadLocation](#FileDownloadLocation).
58-
59-
### Using extension methods
85+
### Using extension methods on `ObjectFile` instance
6086

6187
Various extension methods are enabled from the `ObjectFile` instance, and can be used for easy file access.
6288

89+
These methods are defined in the file MFilesAPI.Extensions/ExtensionMethods/ObjectFileExtensionMethods.cs
90+
6391
#### Passing a physical file path
6492

6593
```csharp
6694
// It is assumed that objectFile and vault are already available/populated.
67-
using(var fileDownload = objectFile.Download(vault, @"C:\temp\mydownload.tmp"))
95+
using(TemporaryFileDownload fileDownload = objectFile.Download(vault, @"C:\temp\mydownload.tmp"))
6896
{
69-
// TODO: Perform operations on physicalFile.
97+
// TODO: Perform operations on physical file.
98+
// The file will be deleted when this instance will be disposed.
7099
}
71100
```
72101

@@ -75,27 +104,10 @@ using(var fileDownload = objectFile.Download(vault, @"C:\temp\mydownload.tmp"))
75104
```csharp
76105
// It is assumed that objectFile and vault are already available/populated.
77106
var physicalFile = new FileInfo("C:\temp\mydownload.tmp");
78-
using(var fileDownload = objectFile.Download(vault, physicalFile))
79-
{
80-
// TODO: Perform operations on physicalFile.
81-
}
82-
```
83-
84-
#### Using a FileDownloadLocation
85-
86-
```csharp
87-
using(var downloadLocation = new FileDownloadLocation())
107+
using(TemporaryFileDownload fileDownload = objectFile.Download(vault, physicalFile))
88108
{
89-
// Set the location to automatically clean on disposal.
90-
// Alternatively we could manually call downloadLocation.CleanTemporaryFiles().
91-
downloadLocation.CleanDirectoryOnDisposal = true;
92-
93-
// It is assumed that objectFile and vault are already available/populated.
94-
// Note: the file will be assigned a guid-based filename with a suitable file extension.
95-
using(var fileDownload = objectFile.Download(vault, downloadLocation))
96-
{
97-
// TODO: Perform operations on physicalFile.
98-
}
109+
// TODO: Perform operations on physical file.
110+
// The file will be deleted when this instance will be disposed.
99111
}
100112
```
101113

@@ -105,39 +117,52 @@ By default the file will be downloaded from the server in block sizes of 4096 by
105117

106118
```csharp
107119
// It is assumed that objectFile and vault are already available/populated.
108-
using(var fileDownload = objectFile.Download(vault, @"C:\temp\mydownload.tmp", blockSize: 8192))
120+
using(TemporaryFileDownload fileDownload = objectFile.Download(vault, @"C:\temp\mydownload.tmp", blockSize: 8192))
109121
{
110-
// TODO: Perform operations on physicalFile.
122+
// BlockSize will be 8192 bytes instead of the default 4096 bytes.
111123
}
112124
```
113125

114126
By default the file will be downloaded in the native format. You can request that the server convert the file to PDF prior to conversion by setting the `fileFormat` in the `Download` methods:
115127

116128
```csharp
117129
// It is assumed that objectFile and vault are already available/populated.
118-
using(var fileDownload = objectFile.Download(vault, @"C:\temp\mydownload.tmp", fileFormat: MFFileFormat.MFFileFormatPDF))
130+
using(TemporaryFileDownload fileDownload = objectFile.Download(vault, @"C:\temp\mydownload.tmp", fileFormat: MFFileFormat.MFFileFormatPDF))
119131
{
120-
// TODO: Perform operations on physicalFile.
132+
// TODO: Perform operations on physical file, which will be a PDF.
121133
}
122134
```
123135

124136

137+
125138
## FileDownloadStream
126139

127-
Allows access to file data from the M-Files vault in a .NET `Stream` structure:
140+
Allows access to file data from the M-Files vault in a .NET `Stream` structure.
141+
142+
This class implements the `IDisposable` interface as it inherits from `System.IO.Stream`.
143+
144+
Instances are either created using the `new` keyword, or via extensions methods on `ObjectFile` instances.
145+
146+
### Using the new keyword
128147

129148
```csharp
130-
// Define the file to write to.
131-
var targetFile = new FileInfo(@"C:\temp\myfile.pdf");
149+
// Read the data as a stream.
150+
using (FileDownloadStream downloadStream = new FileDownloadStream(this.FileToDownload, this.Vault))
151+
{
152+
// TODO use the stream as needed.
153+
}
154+
```
155+
156+
### Using extension methods on `ObjectFile` instance
157+
158+
See also the `OpenRead` and `OpenWrite` extension methods on `ObjectFile` instances.
132159

133-
// Open the target file for writing.
134-
using (var stream = targetFile.OpenWrite())
160+
These methods are defined in the file MFilesAPI.Extensions/ExtensionMethods/ObjectFileExtensionMethods.cs
161+
162+
```csharp
163+
// It is assumed that objectFile and vault are already available/populated.
164+
using(FileDownloadStream downloadStream = objectFile.Download(vault, @"C:\temp\mydownload.tmp"))
135165
{
136-
// Read the data as a stream.
137-
using (var downloadStream = new FileDownloadStream(this.FileToDownload, this.Vault))
138-
{
139-
// Copy the downloaded data to the output stream.
140-
downloadStream.CopyTo(stream);
141-
}
166+
// TODO use the stream as needed.
142167
}
143168
```

0 commit comments

Comments
 (0)