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

Commit c2e7618

Browse files
committed
Added Name and FileName to IFormFile
This commits also gets rid of the name closure in FormFileCollection by interating over the files in the collection instead of using Find and FindAll. Closes #352 and #499
1 parent 9887fe0 commit c2e7618

File tree

5 files changed

+43
-25
lines changed

5 files changed

+43
-25
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ public interface IFormFile
1818

1919
long Length { get; }
2020

21+
string Name { get; }
22+
23+
string FileName { get; }
24+
2125
Stream OpenReadStream();
2226
}
2327
}

src/Microsoft.AspNet.Http/Features/FormFeature.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,10 @@ private async Task<IFormCollection> InnerReadFormAsync(CancellationToken cancell
149149
// Find the end
150150
await section.Body.DrainAsync(cancellationToken);
151151

152-
var file = new FormFile(_request.Body, section.BaseStreamOffset.Value, section.Body.Length)
152+
var name = HeaderUtilities.RemoveQuotes(contentDisposition.Name) ?? string.Empty;
153+
var fileName = HeaderUtilities.RemoveQuotes(contentDisposition.FileName) ?? string.Empty;
154+
155+
var file = new FormFile(_request.Body, section.BaseStreamOffset.Value, section.Body.Length, name, fileName)
153156
{
154157
Headers = new HeaderDictionary(section.Headers),
155158
};

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

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,16 @@ namespace Microsoft.AspNet.Http.Features.Internal
88
{
99
public class FormFile : IFormFile
1010
{
11-
private Stream _baseStream;
12-
private long _baseStreamOffset;
13-
private long _length;
11+
private readonly Stream _baseStream;
12+
private readonly long _baseStreamOffset;
1413

15-
public FormFile(Stream baseStream, long baseStreamOffset, long length)
14+
public FormFile(Stream baseStream, long baseStreamOffset, long length, string name, string fileName)
1615
{
1716
_baseStream = baseStream;
1817
_baseStreamOffset = baseStreamOffset;
19-
_length = length;
18+
Length = length;
19+
Name = name;
20+
FileName = fileName;
2021
}
2122

2223
public string ContentDisposition
@@ -33,14 +34,15 @@ public string ContentType
3334

3435
public IHeaderDictionary Headers { get; set; }
3536

36-
public long Length
37-
{
38-
get { return _length; }
39-
}
37+
public long Length { get; }
38+
39+
public string Name { get; }
40+
41+
public string FileName { get; }
4042

4143
public Stream OpenReadStream()
4244
{
43-
return new ReferenceReadStream(_baseStream, _baseStreamOffset, _length);
45+
return new ReferenceReadStream(_baseStream, _baseStreamOffset, Length);
4446
}
4547
}
4648
}
Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,41 @@
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.Collections.Generic;
5-
using Microsoft.Net.Http.Headers;
66

77
namespace Microsoft.AspNet.Http.Internal
88
{
99
public class FormFileCollection : List<IFormFile>, IFormFileCollection
1010
{
11-
public IFormFile this[string name]
12-
{
13-
get { return GetFile(name); }
14-
}
11+
public IFormFile this[string name] => GetFile(name);
1512

1613
public IFormFile GetFile(string name)
1714
{
18-
return Find(file => string.Equals(name, GetName(file.ContentDisposition)));
15+
foreach (var file in this)
16+
{
17+
if (string.Equals(name, file.Name, StringComparison.OrdinalIgnoreCase))
18+
{
19+
return file;
20+
}
21+
}
22+
23+
return null;
1924
}
2025

2126
public IReadOnlyList<IFormFile> GetFiles(string name)
2227
{
23-
return FindAll(file => string.Equals(name, GetName(file.ContentDisposition)));
24-
}
28+
var files = new List<IFormFile>();
2529

26-
private static string GetName(string contentDisposition)
27-
{
28-
// Content-Disposition: form-data; name="myfile1"; filename="Misc 002.jpg"
29-
ContentDispositionHeaderValue cd;
30-
ContentDispositionHeaderValue.TryParse(contentDisposition, out cd);
31-
return HeaderUtilities.RemoveQuotes(cd?.Name);
30+
foreach (var file in this)
31+
{
32+
if (string.Equals(name, file.Name, StringComparison.OrdinalIgnoreCase))
33+
{
34+
files.Add(file);
35+
}
36+
}
37+
38+
return files;
3239
}
3340
}
3441
}

test/Microsoft.AspNet.Http.Tests/FormFeatureTests.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,8 @@ public async Task ReadFormAsync_MultipartWithFile_ReturnsParsedFormCollection()
215215
Assert.Equal(1, formCollection.Files.Count);
216216

217217
var file = formCollection.Files["myfile1"];
218+
Assert.Equal("myfile1", file.Name);
219+
Assert.Equal("temp.html", file.FileName);
218220
Assert.Equal("text/html", file.ContentType);
219221
Assert.Equal(@"form-data; name=""myfile1""; filename=""temp.html""", file.ContentDisposition);
220222
var body = file.OpenReadStream();

0 commit comments

Comments
 (0)