Skip to content

Commit ee231f4

Browse files
authored
Add HttpPostedFile*. (#260)
Add missing property of File in HttpRequest. Fixes #141
1 parent aa73002 commit ee231f4

File tree

9 files changed

+168
-1
lines changed

9 files changed

+168
-1
lines changed

src/Microsoft.AspNetCore.SystemWebAdapters/Generated/Ref.Standard.cs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,13 +139,37 @@ public sealed partial class HttpFileCollection : System.Collections.Specialized.
139139
internal HttpFileCollection() { }
140140
public string[] AllKeys { get { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");} }
141141
public override int Count { get { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");} }
142+
public System.Web.HttpPostedFile this[int index] { get { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");} }
142143
public System.Web.HttpPostedFile this[string name] { get { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");} }
143144
[System.ObsoleteAttribute("Retrieving Keys is not supported on .NET 6+. Please use the enumerator instead.")]
144145
public override System.Collections.Specialized.NameObjectCollectionBase.KeysCollection Keys { get { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");} }
146+
public System.Web.HttpPostedFile Get(int index) { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");}
145147
public System.Web.HttpPostedFile Get(string name) { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");}
146148
public override System.Collections.IEnumerator GetEnumerator() { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");}
147149
public System.Collections.Generic.IList<System.Web.HttpPostedFile> GetMultiple(string name) { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");}
148150
}
151+
public abstract partial class HttpFileCollectionBase : System.Collections.Specialized.NameObjectCollectionBase
152+
{
153+
protected HttpFileCollectionBase() { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");}
154+
public virtual string[] AllKeys { get { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");} }
155+
public virtual System.Web.HttpPostedFileBase this[int index] { get { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");} }
156+
public virtual System.Web.HttpPostedFileBase this[string name] { get { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");} }
157+
public virtual System.Web.HttpPostedFileBase Get(int index) { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");}
158+
public virtual System.Web.HttpPostedFileBase Get(string name) { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");}
159+
public virtual System.Collections.Generic.IList<System.Web.HttpPostedFileBase> GetMultiple(string name) { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");}
160+
}
161+
public partial class HttpFileCollectionWrapper : System.Web.HttpFileCollectionBase
162+
{
163+
public HttpFileCollectionWrapper(System.Web.HttpFileCollection httpFileCollection) { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");}
164+
public override string[] AllKeys { get { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");} }
165+
public override int Count { get { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");} }
166+
public override System.Web.HttpPostedFileBase this[int index] { get { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");} }
167+
public override System.Web.HttpPostedFileBase this[string name] { get { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");} }
168+
public override System.Web.HttpPostedFileBase Get(int index) { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");}
169+
public override System.Web.HttpPostedFileBase Get(string name) { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");}
170+
public override System.Collections.IEnumerator GetEnumerator() { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");}
171+
public override System.Collections.Generic.IList<System.Web.HttpPostedFileBase> GetMultiple(string name) { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");}
172+
}
149173
public sealed partial class HttpPostedFile
150174
{
151175
internal HttpPostedFile() { }
@@ -154,6 +178,22 @@ internal HttpPostedFile() { }
154178
public string FileName { get { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");} }
155179
public System.IO.Stream InputStream { get { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");} }
156180
}
181+
public abstract partial class HttpPostedFileBase
182+
{
183+
protected HttpPostedFileBase() { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");}
184+
public virtual int ContentLength { get { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");} }
185+
public virtual string ContentType { get { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");} }
186+
public virtual string FileName { get { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");} }
187+
public virtual System.IO.Stream InputStream { get { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");} }
188+
}
189+
public partial class HttpPostedFileWrapper : System.Web.HttpPostedFileBase
190+
{
191+
public HttpPostedFileWrapper(System.Web.HttpPostedFile httpPostedFile) { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");}
192+
public override int ContentLength { get { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");} }
193+
public override string ContentType { get { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");} }
194+
public override string FileName { get { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");} }
195+
public override System.IO.Stream InputStream { get { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");} }
196+
}
157197
public partial class HttpRequest
158198
{
159199
internal HttpRequest() { }
@@ -208,6 +248,7 @@ public abstract partial class HttpRequestBase
208248
public virtual int ContentLength { get { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");} }
209249
public virtual string ContentType { get { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");} set { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");} }
210250
public virtual System.Web.HttpCookieCollection Cookies { get { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");} }
251+
public virtual System.Web.HttpFileCollectionBase Files { get { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");} }
211252
public virtual System.Collections.Specialized.NameValueCollection Headers { get { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");} }
212253
public virtual string HttpMethod { get { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");} }
213254
public virtual System.IO.Stream InputStream { get { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");} }
@@ -245,6 +286,7 @@ public partial class HttpRequestWrapper : System.Web.HttpRequestBase
245286
public override int ContentLength { get { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");} }
246287
public override string ContentType { get { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");} set { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");} }
247288
public override System.Web.HttpCookieCollection Cookies { get { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");} }
289+
public override System.Web.HttpFileCollectionBase Files { get { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");} }
248290
public override System.Collections.Specialized.NameValueCollection Headers { get { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");} }
249291
public override string HttpMethod { get { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");} }
250292
public override System.IO.Stream InputStream { get { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");} }

src/Microsoft.AspNetCore.SystemWebAdapters/Generated/TypeForwards.Framework.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@
2727
[assembly:System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Web.HttpCookieCollection))]
2828
[assembly:System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Web.HttpException))]
2929
[assembly:System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Web.HttpFileCollection))]
30+
[assembly:System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Web.HttpFileCollectionBase))]
31+
[assembly:System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Web.HttpFileCollectionWrapper))]
3032
[assembly:System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Web.HttpPostedFile))]
33+
[assembly:System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Web.HttpPostedFileBase))]
34+
[assembly:System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Web.HttpPostedFileWrapper))]
3135
[assembly:System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Web.HttpRequest))]
3236
[assembly:System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Web.HttpRequestBase))]
3337
[assembly:System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Web.HttpRequestWrapper))]

src/Microsoft.AspNetCore.SystemWebAdapters/HttpFileCollection.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,14 @@ public override IEnumerator GetEnumerator()
4343

4444
public HttpPostedFile? Get(string name) => FormFiles.GetFile(name) is { } file ? new(file) : null;
4545

46+
public HttpPostedFile? Get(int index) => FormFiles[index] is { } file ? new(file) : null;
47+
4648
public IList<HttpPostedFile> GetMultiple(string name) => FormFiles.GetFiles(name) is { Count: > 0 } files ? new ReadOnlyPostedFileCollection(files) : Array.Empty<HttpPostedFile>();
4749

4850
public HttpPostedFile? this[string name] => Get(name);
4951

52+
public HttpPostedFile? this[int index] => Get(index);
53+
5054
private string[] GetKeys()
5155
{
5256
if (FormFiles.Count == 0)
@@ -86,7 +90,7 @@ public HttpPostedFile this[int index]
8690
set => throw new NotSupportedException(Message);
8791
}
8892

89-
private static HttpPostedFile Create(IFormFile file) => new(file);
93+
private static HttpPostedFile Create(IFormFile file) => new HttpPostedFile(file);
9094

9195
public int Count => _other.Count;
9296

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System.Collections.Generic;
5+
using System.Collections.Specialized;
6+
7+
namespace System.Web;
8+
9+
[Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1010:Generic interface should also be implemented", Justification = Constants.ApiFromAspNet)]
10+
[Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1065:Do not raise exceptions in unexpected locations", Justification = Constants.ApiFromAspNet)]
11+
[Diagnostics.CodeAnalysis.SuppressMessage("Naming", "CA1716:Identifiers should not match keywords", Justification = Constants.ApiFromAspNet)]
12+
public abstract class HttpFileCollectionBase : NameObjectCollectionBase
13+
{
14+
[Diagnostics.CodeAnalysis.SuppressMessage("Performance", "CA1819:Properties should not return arrays", Justification = Constants.ApiFromAspNet)]
15+
public virtual string[] AllKeys => throw new NotImplementedException();
16+
17+
public virtual HttpPostedFileBase? this[string name] => throw new NotImplementedException();
18+
19+
public virtual HttpPostedFileBase? this[int index] => throw new NotImplementedException();
20+
21+
public virtual HttpPostedFileBase? Get(int index) => throw new NotImplementedException();
22+
23+
public virtual HttpPostedFileBase? Get(string name) => throw new NotImplementedException();
24+
25+
public virtual IList<HttpPostedFileBase> GetMultiple(string name) => throw new NotImplementedException();
26+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System.Collections;
5+
using System.Collections.Generic;
6+
using System.Linq;
7+
8+
namespace System.Web;
9+
10+
[Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1010:Generic interface should also be implemented", Justification = Constants.ApiFromAspNet)]
11+
public class HttpFileCollectionWrapper : HttpFileCollectionBase
12+
{
13+
private readonly HttpFileCollection _collection;
14+
15+
public HttpFileCollectionWrapper(HttpFileCollection httpFileCollection)
16+
{
17+
ArgumentNullException.ThrowIfNull(httpFileCollection);
18+
19+
_collection = httpFileCollection;
20+
}
21+
22+
public override string[] AllKeys => _collection.AllKeys;
23+
24+
public override int Count => _collection.Count;
25+
26+
public override HttpPostedFileBase? this[string name] => Get(name);
27+
28+
public override HttpPostedFileBase? this[int index] => Get(index);
29+
30+
public override HttpPostedFileBase? Get(string name) =>
31+
_collection[name] is { } file ? new HttpPostedFileWrapper(file) : null;
32+
33+
public override HttpPostedFileBase? Get(int index) =>
34+
_collection[index] is { } file ? new HttpPostedFileWrapper(file) : null;
35+
36+
public override IList<HttpPostedFileBase> GetMultiple(string name)
37+
=> _collection.GetMultiple(name)
38+
.Select(x => (HttpPostedFileBase)new HttpPostedFileWrapper(x))
39+
.ToList()
40+
.AsReadOnly();
41+
42+
public override IEnumerator GetEnumerator() => _collection.GetEnumerator();
43+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System.IO;
5+
6+
namespace System.Web;
7+
8+
[Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1065:Do not raise exceptions in unexpected locations", Justification = Constants.ApiFromAspNet)]
9+
public abstract class HttpPostedFileBase
10+
{
11+
public virtual string FileName => throw new NotImplementedException();
12+
13+
public virtual string ContentType => throw new NotImplementedException();
14+
15+
public virtual int ContentLength => throw new NotImplementedException();
16+
17+
public virtual Stream InputStream => throw new NotImplementedException();
18+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System.IO;
5+
6+
namespace System.Web;
7+
8+
public class HttpPostedFileWrapper : HttpPostedFileBase
9+
{
10+
private readonly HttpPostedFile _file;
11+
12+
public HttpPostedFileWrapper(HttpPostedFile httpPostedFile)
13+
{
14+
ArgumentNullException.ThrowIfNull(httpPostedFile);
15+
16+
_file = httpPostedFile;
17+
}
18+
19+
public override int ContentLength => _file.ContentLength;
20+
21+
public override string ContentType => _file.ContentType;
22+
23+
public override string FileName => _file.FileName;
24+
25+
public override Stream InputStream => _file.InputStream;
26+
}

src/Microsoft.AspNetCore.SystemWebAdapters/HttpRequestBase.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ public abstract class HttpRequestBase
3838

3939
public virtual HttpCookieCollection Cookies => throw new NotImplementedException();
4040

41+
public virtual HttpFileCollectionBase Files => throw new NotImplementedException();
42+
4143
public virtual string? this[string key] => throw new NotImplementedException();
4244

4345
public virtual NameValueCollection Params => throw new NotImplementedException();

src/Microsoft.AspNetCore.SystemWebAdapters/HttpRequestWrapper.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ public override string? ContentType
3737

3838
public override NameValueCollection Headers => _request.Headers;
3939

40+
public override HttpFileCollectionBase Files => new HttpFileCollectionWrapper(_request.Files);
41+
4042
public override string HttpMethod => _request.HttpMethod;
4143

4244
public override Stream InputStream => _request.InputStream;

0 commit comments

Comments
 (0)