代码之家  ›  专栏  ›  技术社区  ›  Kieron

ASP.NET MVC、MVVM和文件上载

  •  2
  • Kieron  · 技术社区  · 15 年前

    我非常喜欢MVVM模式,尤其是在使用ASP.NET MVC框架时(在本例中是V2 Preview 2)。

    但我很好奇是否有人知道在上传文件时如何使用它?

    public class MyViewModel
    {
        public WhatTypeShouldThisBe MyFileUpload { get; set; }
    }
    
    3 回复  |  直到 15 年前
        1
  •  3
  •   Community CDub    8 年前

    我有一个 production asp.net mvc 使用我编写的jquery多文件上载程序运行的站点。 This answer 其中包含了大部分代码,因此应该可以从MVC中的上载开始。问题实际上是关于将上载的文件存储在 Stream 我用的是 byte[] 但您将在答案中看到我的代码如何应用于这两种情况。

        2
  •  1
  •   magallanes    15 年前

    通常 httpfilecollection(httpfilecollection) 如果您使用的是标准组件(system.io.path),就足够了。

    注意 httpfilecollection(httpfilecollection) 是一个集合 HtpPoestDebug文件 ,也就是说,您可以一次上载多个文件。

        3
  •  1
  •   tuanvt    15 年前

    我认为字节[]足以存储上载的文件,例如,在我的上载操作中,我会这样做:

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

    希望它能帮助一些人。