Mercurial > altnet-hispano
comparison 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 |
comparison
equal
deleted
inserted
replaced
274:72a96459f910 | 275:bf993f99cee3 |
---|---|
1 using System; | |
2 using System.IO; | |
3 using System.Web; | |
4 using System.Web.Mvc; | |
5 | |
6 namespace Agendas.Web.Tests.Controllers | |
7 { | |
8 public class HttpPostedFileBaseModelBinder : IModelBinder | |
9 { | |
10 public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) | |
11 { | |
12 var fileDummy = new HttpFileDummy(((FormCollection)bindingContext.ValueProvider)[bindingContext.ModelName], "image/jpg"); | |
13 if (string.IsNullOrEmpty(fileDummy.FileName)) | |
14 { | |
15 fileDummy = null; | |
16 } | |
17 return fileDummy; | |
18 } | |
19 } | |
20 | |
21 public class HttpFileDummy : HttpPostedFileBase, IDisposable | |
22 { | |
23 public override int ContentLength | |
24 { | |
25 get { return (int)InputStream.Length; } | |
26 } | |
27 | |
28 private readonly string _contentType; | |
29 public override string ContentType | |
30 { | |
31 get { return _contentType; } | |
32 } | |
33 | |
34 private readonly string _fileName; | |
35 public override string FileName | |
36 { | |
37 get { return _fileName; } | |
38 } | |
39 | |
40 private FileStream _stream; | |
41 public override Stream InputStream | |
42 { | |
43 get | |
44 { | |
45 return _stream ?? (_stream = new FileStream(FileName, FileMode.Open, | |
46 FileAccess.Read, FileShare.Read)); | |
47 } | |
48 } | |
49 | |
50 public HttpFileDummy(string fileName, string contentType) | |
51 { | |
52 _contentType = contentType; | |
53 _fileName = fileName; | |
54 } | |
55 | |
56 public void Dispose() | |
57 { | |
58 if (_stream != null) | |
59 { | |
60 try { _stream.Dispose(); } | |
61 finally { _stream = null; } | |
62 } | |
63 } | |
64 | |
65 public override void SaveAs(string filename) | |
66 { | |
67 File.WriteAllBytes(filename, File.ReadAllBytes(FileName)); | |
68 } | |
69 } | |
70 } |