如何绑定名为"file []"的已发布数据;到MVC模型? [英] How to bind posted data named "file[]" to an MVC model?

查看:82
本文介绍了如何绑定名为"file []"的已发布数据;到MVC模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Redactor 作为HTML编辑器,该编辑器具有

I am using Redactor as an HTML editor, which has a component for uploading images and files.

Redactor负责客户端位,我需要提供服务器端上传功能.

Redactor takes care of the client side bit, and I need to provide the server side upload functionality.

如果我在控制器中使用Request.Files,则可以正常进行上传.

I have no problem getting the uploads to work if I use Request.Files in the controller.

但是我想将发布的文件绑定到模型,但是我似乎无法做到这一点,因为与它们一起发送的参数是files[]-名称中带有方括号.

But I would like to bind the posted files to a Model, and I seem unable to do this, because the parameter they are sent with is files[] - with square brackets in the name.

我的问题:

是否可以将发布的"file[]"绑定到MVC模型?这是无效的属性名称,仅使用file无效.

Is it possible to bind the posted "file[]" to an MVC model? It's an invalid property name, and using file alone doesn't work.

此文件输入如下所示.我可以指定除file之外的其他名称,但是Redactor会在其末尾添加[],而不管其名称如何.

This file input looks like this. I can specify a name other than file, but Redactor adds [] to the end, regardless of the name.

<input type="file" name="file" multiple="multiple" style="display: none;">

我正在尝试绑定到这样的属性:

I am trying to bind to a property like this:

public HttpPostedFileBase[] File { get; set; }

当我看到上传发生时,我在请求中看到了这一点(我认为编辑器可能在幕后添加了方括号):

When I watch the upload take place, I see this in the request (I presume that redactor may be adding the square brackets behind the scenes):

Content-Disposition: form-data; name="file[]"; filename="my-image.jpg"

也相关:

Redactor始终发送内容类型为multipart/form-data的上传请求.因此,您无需在任何地方添加此enctype

Redactor always sends the uploading request with content-type as multipart/form-data. So you don't need to add this enctype anywhere

推荐答案

您应该创建一个自定义模型绑定程序,以将上传的文件绑定到一个属性. 首先创建一个具有HttpPostedFileBase[]属性的模型

You should create a custom model binder to bind uploaded files to one property. First create a model with a HttpPostedFileBase[] property

public class RactorModel
{
    public HttpPostedFileBase[] Files { get; set; }
}

然后执行DefaultModelBinder并覆盖BindProperty

public class RactorModelBinder : DefaultModelBinder
{
    protected override void BindProperty(ControllerContext controllerContext, ModelBindingContext bindingContext, PropertyDescriptor propertyDescriptor)
    {
        int len = controllerContext.HttpContext.Request.Files.AllKeys.Length;

        if (len > 0)
        {
            if (propertyDescriptor.PropertyType == typeof(HttpPostedFileBase[]))
            {
                string formName = string.Format("{0}[]", propertyDescriptor.Name);
                HttpPostedFileBase[] files = new HttpPostedFileBase[len];
                for (int i = 0; i < len; i++)
                {
                    files[i] = controllerContext.HttpContext.Request.Files[i];
                }

                propertyDescriptor.SetValue(bindingContext.Model, files);
                return;
            }
        }

        base.BindProperty(controllerContext, bindingContext, propertyDescriptor);
    }
}

另外,您应该将活页夹提供程序添加到您的项目,然后在global.asax中注册它

Also you should add binder provider to your project, then register it in global.asax

public class RactorModenBinderProvider : IModelBinderProvider
{
    public IModelBinder GetBinder(Type modelType)
    {
        if (modelType == typeof(RactorModel))
        {
            return new RactorModelBinder();
        }

        return null;
    }
}
...
ModelBinderProviders.BinderProviders.Insert(0, new RactorModenBinderProvider());

这不是一般的解决方案,但我想您明白了.

this isn't a general solution, but I guess you get the point.

这篇关于如何绑定名为"file []"的已发布数据;到MVC模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆