MVC: Handling file uploads

by John Rayner 16. December 2011 21:59

ASP.Net MVC has some great abstractions over HTTP and the various Http classes, but one area which is currently lacking is around processing of file uploads.  Uploaded files are (still) available on the Request.Files collection and some posts suggest that you should go down this route.  And for testing, Scott Hanselman has provided some (fairly scary) mocking code.

Now Phil Haack has suggested a few improvements on this, but it’s still not as easy to test as I’d like (I’m a lazy developer, really) – I like to avoid mocks when I can since they can easily obscure the intention of the test.  Fortunately MVC model binders came to my rescue here.  Note that my web UI is only ever submitting one file.

DTO class:

public class UploadedFile
{
    public int Length { get; set; }
    public string ContentType { get; set; }
    public string FileName { get; set; }
    public Stream Stream { get; set; }
}

Model binder:

public class FileUploadBinder : IModelBinder
{
    public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        var postedFile = controllerContext.HttpContext.Request.Files[0];
        if (postedFile == null) return null;
        return new UploadedFile
                   {
                       Length = postedFile.ContentLength,
                       ContentType = postedFile.ContentType,
                       FileName = postedFile.FileName,
                       Stream = postedFile.InputStream
                   };
    }
}

Controller action method:

[HttpPost, Authorize]
public ActionResult PeformFullUpload(string dataSourceName, 
            [ModelBinder(typeof(FileUploadBinder))] UploadedFile uploadedFile)

Test code:

[When("the user submits a file")]
public void PerformFileUpload()
{
    var uploadedFile = new UploadedFile{ Stream = _memoryStream};
    _viewResult = _controller.PeformFullUpload(_dataSourceName, _uploadedFile) as ViewResult;
}

Now that’s how I like my tests.   Smile

Tags:
Categories:

Comments

billet pas cher avion
billet pas cher avion United States on 12/17/2011 10:39:48 AM

merci d' avoir posté cette info !!!!

pingback
codetopreload.com on 12/18/2011 10:08:14 PM

Pingback from codetopreload.com

MVC: Handling file uploads | Code to Preload

sw.onlinecasino-payments.com
sw.onlinecasino-payments.com United States on 12/23/2011 1:58:13 PM

A fine post, both quality and quantity. Thank you!

pingback
atm-lol.net on 12/24/2011 9:38:15 AM

Pingback from atm-lol.net

horoscopes

pingback
ezfreereport.com on 12/25/2011 4:50:41 AM

Pingback from ezfreereport.com

yasmin court case

billet pas cher d'avion
billet pas cher d'avion United States on 12/28/2011 10:11:59 PM

Grand merci pour le post de cet article ?!

Ethylotest
Ethylotest United States on 12/29/2011 1:31:29 AM

Grand merci pour ce post Smile

Teddy Piquette
Teddy Piquette United States on 12/30/2011 6:36:42 PM

Thanks for sharing.

Donette Reiners
Donette Reiners United States on 12/30/2011 7:34:18 PM

This is good thing to share!!!Thank you for this info

Comments are closed