How to bind posted data named “file[]” to an MVC model?

吃可爱长大的小学妹 提交于 2019-12-01 04:18:29

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

then implement DefaultModelBinder and override 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);
    }
}

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.

I encountered similar problem during the integration of jQuery.filer in an ASP.NET MVC project. As jQuery.filer adds "[]" to the end of name attribute of input (i.e. from files to files[]), I had to change the value of name attribute manually as shown below:

$('#FileUpload').attr('name', 'FileUpload');

Here is my approach used in some of project via AJAX and working without any problem. You might give a try and let me know if it works:

ViewModel:

[Display(Name = "Attachments")]
[DataType(DataType.Upload)]
public IEnumerable<HttpPostedFileBase> FileUpload { get; set; }


View:

@model ViewModel

@using (Html.BeginForm("Insert", "Controller", FormMethod.Post, 
    new { id = "frmCreate", enctype = "multipart/form-data" })) 
{   
    @Html.TextBoxFor(m => m.FileUpload, new { type = "file", multiple = "multiple" })
    <button id="btnSubmit" onclick="insert(event)" type="button">Save</button>
}    

<script>     
function insert(event) {     
    event.preventDefault();

    //As jQuery.filer adds "[]" to the end of name attribute of input (i.e. from files to files[])
    //we have to change the value of name attribute manually
    $('#FileUpload').attr('name', 'FileUpload');        
    var formdata = new FormData($('#frmCreate').get(0)); 

    $.ajax({
        type: "POST",
        url: '@Url.Action("Insert", "Cotroller")',
        cache: false,
        dataType: "json",
        data: formdata,

        /* If you are uploading files, then processData and contentType must be set 
        to falsein order for FormData to work (otherwise comment out both of them) */
        processData: false, 
        contentType: false, 

        success: function (response, textStatus, XMLHttpRequest) {
            //...
        }
    });
};

$(document).ready(function () {         
    $('#FileUpload').filer({        
        //code omitted for brevity
    });  
});  
</script>


Controller:

public JsonResult Insert([Bind(Exclude = null)] ViewModel model)
{
    if (ModelState.IsValid)
    {   
        List<FileAttachment> fa = new List<FileAttachment>();
        if (model.FileUpload != null)
        {
            FileAttachment fileAttachment = new FileAttachment //entity model
            {
                Created = DateTime.Now,
                FileMimeType = upload.ContentType,
                FileData = new byte[upload.ContentLength],
                FileName = upload.FileName,
                AuthorId = 1
            };
            upload.InputStream.Read(fileAttachment.FileData, 0, upload.ContentLength);
            fa.Add(fileAttachment);
        }

        //code omitted for brevity
        repository.SaveExperimentWithAttachment(model, fa);
        return Json(new { success = true, message = "Record has been created." });
    }
    // If we got this far, something failed, redisplay form
    return Json(new { success = false, message = "Please check the form and try again." });
}

Hope this helps...

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!