|
1 | 1 | // Copyright (c) .NET Foundation. All rights reserved.
|
2 | 2 | // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
3 | 3 |
|
| 4 | +using System; |
4 | 5 | using System.IO;
|
5 | 6 | using System.Threading;
|
6 | 7 | using System.Threading.Tasks;
|
@@ -74,27 +75,35 @@ public Stream OpenReadStream()
|
74 | 75 | /// <summary>
|
75 | 76 | /// Saves the contents of the uploaded file.
|
76 | 77 | /// </summary>
|
77 |
| - /// <param name="path">The path of the file to create.</param> |
78 |
| - public void SaveAs(string path) |
| 78 | + /// <param name="target">The stream to save the file to.</param> |
| 79 | + public void Save(Stream target) |
79 | 80 | {
|
80 |
| - using (var fileStream = File.Create(path, DefaultBufferSize)) |
| 81 | + if (target == null) |
81 | 82 | {
|
82 |
| - var inputStream = OpenReadStream(); |
83 |
| - inputStream.CopyTo(fileStream, DefaultBufferSize); |
| 83 | + throw new ArgumentNullException(nameof(target)); |
| 84 | + } |
| 85 | + |
| 86 | + using (var readStream = OpenReadStream()) |
| 87 | + { |
| 88 | + readStream.CopyTo(target, DefaultBufferSize); |
84 | 89 | }
|
85 | 90 | }
|
86 | 91 |
|
87 | 92 | /// <summary>
|
88 | 93 | /// Asynchronously saves the contents of the uploaded file.
|
89 | 94 | /// </summary>
|
90 |
| - /// <param name="path">The path of the file to create.</param> |
| 95 | + /// <param name="target">The stream to save the file to.</param> |
91 | 96 | /// <param name="cancellationToken"></param>
|
92 |
| - public async Task SaveAsAsync(string path, CancellationToken cancellationToken = default(CancellationToken)) |
| 97 | + public async Task SaveAsync(Stream target, CancellationToken cancellationToken = default(CancellationToken)) |
93 | 98 | {
|
94 |
| - using (var fileStream = File.Create(path, DefaultBufferSize, FileOptions.Asynchronous)) |
| 99 | + if (target == null) |
| 100 | + { |
| 101 | + throw new ArgumentNullException(nameof(target)); |
| 102 | + } |
| 103 | + |
| 104 | + using (var readStream = OpenReadStream()) |
95 | 105 | {
|
96 |
| - var inputStream = OpenReadStream(); |
97 |
| - await inputStream.CopyToAsync(fileStream, DefaultBufferSize, cancellationToken); |
| 106 | + await readStream.CopyToAsync(target, DefaultBufferSize, cancellationToken); |
98 | 107 | }
|
99 | 108 | }
|
100 | 109 | }
|
|
0 commit comments