diff Agendas/trunk/src/Agendas.Web.Tests/Controllers/HttpPostedFileBaseModelBinder.cs @ 275:bf993f99cee3

Ticket #123: Patrocinadores de las vans.
author juanjose.montesdeocaarbos
date Wed, 14 Dec 2011 08:15:44 -0300
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Agendas/trunk/src/Agendas.Web.Tests/Controllers/HttpPostedFileBaseModelBinder.cs	Wed Dec 14 08:15:44 2011 -0300
@@ -0,0 +1,70 @@
+using System;
+using System.IO;
+using System.Web;
+using System.Web.Mvc;
+
+namespace Agendas.Web.Tests.Controllers
+{
+	public class HttpPostedFileBaseModelBinder : IModelBinder
+	{
+		public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
+		{
+			var fileDummy = new HttpFileDummy(((FormCollection)bindingContext.ValueProvider)[bindingContext.ModelName], "image/jpg");
+			if (string.IsNullOrEmpty(fileDummy.FileName))
+			{
+				fileDummy = null;
+			}
+			return fileDummy;
+		}
+	}
+
+	public class HttpFileDummy : HttpPostedFileBase, IDisposable
+	{
+		public override int ContentLength
+		{
+			get { return (int)InputStream.Length; }
+		}
+
+		private readonly string _contentType;
+		public override string ContentType
+		{
+			get { return _contentType; }
+		}
+
+		private readonly string _fileName;
+		public override string FileName
+		{
+			get { return _fileName; }
+		}
+
+		private FileStream _stream;
+		public override Stream InputStream
+		{
+			get
+			{
+				return _stream ?? (_stream = new FileStream(FileName, FileMode.Open,
+				                                            FileAccess.Read, FileShare.Read));
+			}
+		}
+
+		public HttpFileDummy(string fileName, string contentType)
+		{
+			_contentType = contentType;
+			_fileName = fileName;
+		}
+
+		public void Dispose()
+		{
+			if (_stream != null)
+			{
+				try { _stream.Dispose(); }
+				finally { _stream = null; }
+			}
+		}
+
+		public override void SaveAs(string filename)
+		{
+			File.WriteAllBytes(filename, File.ReadAllBytes(FileName));
+		}
+	}
+}