view Agendas/trunk/src/Agendas.Web.Tests/Controllers/HttpPostedFileBaseModelBinder.cs @ 302:11dddcc9a862 tip

Historico de Eventos, no muestra bien la Url del Patrocinador.
author juanjose.montesdeocaarbos
date Tue, 14 Aug 2012 21:54:30 -0300
parents bf993f99cee3
children
line wrap: on
line source

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));
		}
	}
}