-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHttpPostedFileBaseExtensions.cs
110 lines (109 loc) · 3.34 KB
/
HttpPostedFileBaseExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Threading.Tasks;
using System.Web;
namespace NetFrameworkHelper
{
/// <summary>
/// Extension methods for handling HttpPostedFileBase files.
/// </summary>
public static class HttpPostedFileBaseExtensions
{
/// <summary>
/// Convert a HttpPostedFileBase to an byte array. To compress the byte array, specify true when calling the extension.
/// </summary>
/// <param name="file">Posted file to apply this to</param>
/// <returns>byte[]</returns>
public static byte[] ConvertToBytes(this HttpPostedFileBase file)
{
if (file == null)
{
throw new ArgumentNullException(nameof(file), "The passed in file parameter was null.");
}
return file.ConvertToBytes(false);
}
/// <summary>
/// Convert a HttpPostedFileBase to an byte array. To compress the byte array, specify true when calling the extension.
/// </summary>
/// <param name="file">Posted file to apply this to</param>
/// <param name="compress">Set to true to compress the returned byte array. Default is false.</param>
/// <returns>byte[]</returns>
public static byte[] ConvertToBytes(this HttpPostedFileBase file, bool compress = false)
{
if (file == null)
{
throw new ArgumentNullException(nameof(file), "The passed in file parameter was null.");
}
BinaryReader rdr = new BinaryReader(file.InputStream);
byte[] byteArr = rdr.ReadBytes(file.ContentLength);
rdr.Dispose();
if (compress)
{
return byteArr.Compress();
}
return byteArr;
}
/// <summary>
/// Convert a list of HttpPostedFileBase to a List of Images. Can specify to convert the images. Default is false.
/// </summary>
/// <param name="files">List of HttpPostedFileBase to run this against</param>
/// <returns></returns>
public static List<Image> ToImageList(this List<HttpPostedFileBase> files)
{
if (files == null)
{
throw new ArgumentNullException(nameof(files), "No files passed in.");
}
List<Image> images = new List<Image>();
Parallel.ForEach(files, file =>
{
images.Add(file.ToImage());
});
return images;
}
/// <summary>
/// Convert <see cref="HttpPostedFileBase"/> to Image. To get Image meta data, cast Image.Tags to <see cref="ImageMetaData"/>
/// </summary>
/// <param name="file">file to apply this method to.</param>
/// <returns>Image</returns>
public static Image ToImage(this HttpPostedFileBase file)
{
if (file == null)
{
throw new ArgumentNullException(nameof(file), "File passed was null");
}
Image img = Image.FromStream(file.InputStream, true, true);
img.Tag = new ImageMetaData
{
ImageName = file.FileName,
ImageExtension = file.FileName.GetFileExtension(),
MimeType = file.FileName.GetMimeFromFileName()
};
return img;
}
/// <summary>
/// Gets file extensions of files.
/// </summary>
/// <param name="fileName"></param>
/// <returns>string with the file extension</returns>
private static string GetFileExtension(this string fileName)
{
if (fileName == null)
{
throw new ArgumentNullException(nameof(fileName), "File name not passed in");
}
string extension;
try
{
extension = Path.GetExtension(fileName);
}
catch (ArgumentException)
{
extension = fileName.Mid(fileName.LastIndexOf('.'));
}
return extension;
}
}
}