Fixed content-disposition quoted filename bug

This commit is contained in:
Ajay Bhargav Baaskaran 2015-01-19 11:51:16 -08:00
parent d34554e3ff
commit 67b078862e
2 changed files with 32 additions and 2 deletions

View File

@ -62,12 +62,13 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
// If there is an <input type="file" ... /> in the form and is left blank.
if (parsedContentDisposition == null ||
(file.Length == 0 && string.IsNullOrEmpty(parsedContentDisposition.FileName)))
(file.Length == 0 &&
string.IsNullOrEmpty(HeaderUtilities.RemoveQuotes(parsedContentDisposition.FileName))))
{
continue;
}
var modelName = parsedContentDisposition.Name;
var modelName = HeaderUtilities.RemoveQuotes(parsedContentDisposition.Name);
if (modelName.Equals(bindingContext.ModelName, StringComparison.OrdinalIgnoreCase))
{
postedFiles.Add(file);

View File

@ -5,6 +5,7 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNet.Http;
@ -36,6 +37,26 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
Assert.Equal(2, files.Count);
}
[Fact]
public async Task FormFileModelBinder_FilesWithQuotedContentDisposition_BindSuccessful()
{
// Arrange
var formFiles = new FormFileCollection();
formFiles.Add(GetMockFormFileWithQuotedContentDisposition("file", "file1.txt"));
formFiles.Add(GetMockFormFileWithQuotedContentDisposition("file", "file2.txt"));
var httpContext = GetMockHttpContext(GetMockFormCollection(formFiles));
var bindingContext = GetBindingContext(typeof(IEnumerable<IFormFile>), httpContext);
var binder = new FormFileModelBinder();
// Act
var result = await binder.BindModelAsync(bindingContext);
// Assert
Assert.True(result);
var files = Assert.IsAssignableFrom<IList<IFormFile>>(bindingContext.Model);
Assert.Equal(2, files.Count);
}
[Fact]
public async Task FormFileModelBinder_ExpectSingleFile_BindFirstFile()
{
@ -169,6 +190,14 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
.Returns(string.Format("form-data; name={0}; filename={1}", modelName, filename));
return formFile.Object;
}
private static IFormFile GetMockFormFileWithQuotedContentDisposition(string modelName, string filename)
{
var formFile = new Mock<IFormFile>();
formFile.Setup(f => f.ContentDisposition)
.Returns(string.Format("form-data; name=\"{0}\"; filename=\"{1}\"", modelName, filename));
return formFile.Object;
}
}
}