开发者

ASP.NET MVC, MVVM and file uploads

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; se开发者_如何学编程t; }
}


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜