views:

357

answers:

3

Hi,

I'm big fan of the MVVM pattern, in particular while using the ASP.NET MVC Framework (in this case v2 preview 2).

But I'm curious if anyone knows how to use it when doing file uploads?

public class MyViewModel
{
    public WhatTypeShouldThisBe MyFileUpload { get; set; }
}
+1  A: 

Hi there:

Usually HttpFileCollection is enough if you are using the standard component (System.IO.Path).

take note that HttpFileCollection is a collection of HttpPostedFile, i.e. you can upload many files at once.

magallanes
It is, but it kind of breaks the MVVM pattern - having everything in one place. I suppose the model could check the http context itself, but that somehow smells all wrong.
Kieron
+1  A: 

Hello. I would think byte[] would be enough to store the uploaded file, for example in my upload action, I would do sth like this:

        foreach (string file in Request.Files)
        {
            HttpPostedFileBase hpf = Request.Files(file) as HttpPostedFileBase;
            if (hpf.ContentLength == 0)
            {
                continue;
            }


            //This would be the part to get the data and save it to the view
            byte[] origImageData = new byte[(int)hpf.ContentLength - 1 + 1];
            hpf.InputStream.Read(origImageData, 0, (int)hpf.ContentLength);


        }

Hope it helps some how.

tuanvt
+3  A: 

I have a production asp.net mvc site running with a jQuery multi-file uploader that i wrote. This answer has most of the code included in it so that should get you started with uploads in MVC. The question actually asks about storing the uploaded file in a Stream and i use a byte[], but you'll see in the answer how my code can apply to both scenarios.

cottsak