Skip to content
This repository was archived by the owner on Nov 20, 2018. It is now read-only.

Commit 7a5208a

Browse files
committed
Changed SaveAs[Async](string) to CopyTo[Async](Stream)
1 parent 02363da commit 7a5208a

File tree

2 files changed

+27
-18
lines changed

2 files changed

+27
-18
lines changed

src/Microsoft.AspNetCore.Http.Abstractions/IFormFile.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,16 @@ public interface IFormFile
4848
Stream OpenReadStream();
4949

5050
/// <summary>
51-
/// Saves the contents of the uploaded file.
51+
/// Copies the contents of the uploaded file to the <paramref name="target"/> stream.
5252
/// </summary>
53-
/// <param name="path">The path of the file to create.</param>
54-
void SaveAs(string path);
53+
/// <param name="target">The stream to copy the file contents to.</param>
54+
void CopyTo(Stream target);
5555

5656
/// <summary>
57-
/// Asynchronously saves the contents of the uploaded file.
57+
/// Asynchronously copies the contents of the uploaded file to the <paramref name="target"/> stream.
5858
/// </summary>
59-
/// <param name="path">The path of the file to create.</param>
59+
/// <param name="target">The stream to copy the file contents to.</param>
6060
/// <param name="cancellationToken"></param>
61-
Task SaveAsAsync(string path, CancellationToken cancellationToken = default(CancellationToken));
61+
Task CopyToAsync(Stream target, CancellationToken cancellationToken = default(CancellationToken));
6262
}
6363
}

src/Microsoft.AspNetCore.Http/Features/FormFile.cs

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4+
using System;
45
using System.IO;
56
using System.Threading;
67
using System.Threading.Tasks;
@@ -72,29 +73,37 @@ public Stream OpenReadStream()
7273
}
7374

7475
/// <summary>
75-
/// Saves the contents of the uploaded file.
76+
/// Copies the contents of the uploaded file to the <paramref name="target"/> stream.
7677
/// </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 copy the file contents to.</param>
79+
public void CopyTo(Stream target)
7980
{
80-
using (var fileStream = File.Create(path, DefaultBufferSize))
81+
if (target == null)
8182
{
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);
8489
}
8590
}
8691

8792
/// <summary>
88-
/// Asynchronously saves the contents of the uploaded file.
93+
/// Asynchronously copies the contents of the uploaded file to the <paramref name="target"/> stream.
8994
/// </summary>
90-
/// <param name="path">The path of the file to create.</param>
95+
/// <param name="target">The stream to copy the file contents to.</param>
9196
/// <param name="cancellationToken"></param>
92-
public async Task SaveAsAsync(string path, CancellationToken cancellationToken = default(CancellationToken))
97+
public async Task CopyToAsync(Stream target, CancellationToken cancellationToken = default(CancellationToken))
9398
{
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())
95105
{
96-
var inputStream = OpenReadStream();
97-
await inputStream.CopyToAsync(fileStream, DefaultBufferSize, cancellationToken);
106+
await readStream.CopyToAsync(target, DefaultBufferSize, cancellationToken);
98107
}
99108
}
100109
}

0 commit comments

Comments
 (0)