Samples:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Request.Files.Each( | |
(file, name) => model.otherLogos = model.otherLogos.Concat(new string[] {file.FileName}).ToArray(), | |
"otherLogos" | |
); | |
Request.Files.Each( | |
(file, name) => file.SaveAs(String.Format("{0}/{2}", rootPath, file.FileName)), | |
"otherLogos", | |
new Regex(".*(?<!exe|php|aspx|asp|cs|vp|dll|scr)$") // Prevent uploading specified exstentions. | |
); |
Source Code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Text.RegularExpressions; | |
using System.Web; | |
namespace | |
{ | |
public static class HttpFileCollectionExtensions | |
{ | |
/// <summary> | |
/// Loop through each file and raise a callback function. | |
/// </summary> | |
/// <param name="self">File Collection to loop through.</param> | |
/// <param name="callback">Callback function to call and pass file name and form field name.</param> | |
/// <param name="fieldFilter">If provided only include files posted to this form field.</param> | |
/// <param name="fileNameFilter">If provided only include files matching this expression.</param> | |
public static void Each(this HttpFileCollection self, Action<HttpPostedFile, string> callback, string fieldFilter = "", Regex fileNameFilter = null) | |
{ | |
for (int i = 0; i < self.Count; i++) | |
{ | |
if (String.IsNullOrEmpty(fieldFilter) || self.Keys[i] == fieldFilter) | |
{ | |
if (fileNameFilter == null || fileNameFilter.IsMatch(self[i].FileName)) | |
{ | |
callback.Invoke(self[i], self.Keys[i]); | |
} | |
} | |
} | |
} | |
} | |
} |