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

Add Name and FileName to IFormFile #500

Merged
merged 1 commit into from
Dec 4, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/Microsoft.AspNet.Http.Abstractions/IFormFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ public interface IFormFile

long Length { get; }

string Name { get; }

string FileName { get; }

Stream OpenReadStream();
}
}
5 changes: 4 additions & 1 deletion src/Microsoft.AspNet.Http/Features/FormFeature.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,10 @@ private async Task<IFormCollection> InnerReadFormAsync(CancellationToken cancell
// Find the end
await section.Body.DrainAsync(cancellationToken);

var file = new FormFile(_request.Body, section.BaseStreamOffset.Value, section.Body.Length)
var name = HeaderUtilities.RemoveQuotes(contentDisposition.Name) ?? string.Empty;
var fileName = HeaderUtilities.RemoveQuotes(contentDisposition.FileName) ?? string.Empty;

var file = new FormFile(_request.Body, section.BaseStreamOffset.Value, section.Body.Length, name, fileName)
{
Headers = new HeaderDictionary(section.Headers),
};
Expand Down
22 changes: 12 additions & 10 deletions src/Microsoft.AspNet.Http/Features/FormFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@ namespace Microsoft.AspNet.Http.Features.Internal
{
public class FormFile : IFormFile
{
private Stream _baseStream;
private long _baseStreamOffset;
private long _length;
private readonly Stream _baseStream;
private readonly long _baseStreamOffset;

public FormFile(Stream baseStream, long baseStreamOffset, long length)
public FormFile(Stream baseStream, long baseStreamOffset, long length, string name, string fileName)
{
_baseStream = baseStream;
_baseStreamOffset = baseStreamOffset;
_length = length;
Length = length;
Name = name;
FileName = fileName;
}

public string ContentDisposition
Expand All @@ -33,14 +34,15 @@ public string ContentType

public IHeaderDictionary Headers { get; set; }

public long Length
{
get { return _length; }
}
public long Length { get; }

public string Name { get; }

public string FileName { get; }

public Stream OpenReadStream()
{
return new ReferenceReadStream(_baseStream, _baseStreamOffset, _length);
return new ReferenceReadStream(_baseStream, _baseStreamOffset, Length);
}
}
}
35 changes: 21 additions & 14 deletions src/Microsoft.AspNet.Http/FormFileCollection.cs
Original file line number Diff line number Diff line change
@@ -1,34 +1,41 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using Microsoft.Net.Http.Headers;

namespace Microsoft.AspNet.Http.Internal
{
public class FormFileCollection : List<IFormFile>, IFormFileCollection
{
public IFormFile this[string name]
{
get { return GetFile(name); }
}
public IFormFile this[string name] => GetFile(name);

public IFormFile GetFile(string name)
{
return Find(file => string.Equals(name, GetName(file.ContentDisposition)));
foreach (var file in this)
{
if (string.Equals(name, file.Name, StringComparison.OrdinalIgnoreCase))
{
return file;
}
}

return null;
}

public IReadOnlyList<IFormFile> GetFiles(string name)
{
return FindAll(file => string.Equals(name, GetName(file.ContentDisposition)));
}
var files = new List<IFormFile>();

private static string GetName(string contentDisposition)
{
// Content-Disposition: form-data; name="myfile1"; filename="Misc 002.jpg"
ContentDispositionHeaderValue cd;
ContentDispositionHeaderValue.TryParse(contentDisposition, out cd);
return HeaderUtilities.RemoveQuotes(cd?.Name);
foreach (var file in this)
{
if (string.Equals(name, file.Name, StringComparison.OrdinalIgnoreCase))
{
files.Add(file);
}
}

return files;
}
}
}
2 changes: 2 additions & 0 deletions test/Microsoft.AspNet.Http.Tests/FormFeatureTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,8 @@ public async Task ReadFormAsync_MultipartWithFile_ReturnsParsedFormCollection()
Assert.Equal(1, formCollection.Files.Count);

var file = formCollection.Files["myfile1"];
Assert.Equal("myfile1", file.Name);
Assert.Equal("temp.html", file.FileName);
Assert.Equal("text/html", file.ContentType);
Assert.Equal(@"form-data; name=""myfile1""; filename=""temp.html""", file.ContentDisposition);
var body = file.OpenReadStream();
Expand Down