简体   繁体   English

ASP.NET WebAPI绑定EF模型与文件

[英]asp.net webapi binding ef model with file

I have a webapi project and cannot figure out a nice way to bind the uploaded file content. 我有一个webapi项目,无法找到绑定上传文件内容的好方法。

I have an entity framework model as follows: 我有一个如下的实体框架模型:

public class Person
{
    public int Id { get; set; }
    public string Name { get; set; }
    public UploadedFileId UploadedFileId { get; set; }
    public virtual UploadedFile UploadedFile { get; set; }
}

Where uploaded file is as follows: 上传的文件如下:

public class UploadedFile
{
    public int Id { get; set; }
    public byte[] Content { get; set; }
    public string FileName { get; set; }
}

I have a SPA on the client with the form uploading the file, 我在客户端上有一个SPA,其中包含上传文件的表单,

In the controller I try get the model binding working with the file upload, but neither of my try works. 在控制器中,我尝试使模型绑定与文件上传一起使用,但是我的尝试均无效。

Version 1: 版本1:

using System.Web.Http;

...

public IHttpActionResult Post(Person entity) {
    /* some code */
}

This is obviously not working, although the model itself is null, which I do not understand why. 尽管模型本身为null,但我不明白为什么,但这显然不起作用。 I was expecting it will bind the properties and just ignoring the file. 我期望它会绑定属性,而只是忽略文件。

Version 2: 版本2:

public IHttpActionResult Post(Person entity, HttpPostedFileBase somefile) {
    /* some code */
}

Also tried with different name in the form input on client side (file, fileUpload, files, HttpPostedFile, HttpPostedFileBase) 还尝试在客户端输入的表单中使用其他名称(文件,fileUpload,文件,HttpPostedFile,HttpPostedFileBase)

Version 3: 版本3:

public IHttpActionResult Post(HttpPostedFileBase somefile) {
    /* some code */
}

somefile is null here as well. somefile在这里也为null。

Version 4: Tried to create a binding 版本4:尝试创建绑定

public class UploadedFilesModelBinder : IModelBinder
{
    public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        HttpPostedFileBase file =controllerContext.HttpContext.Request.Files[bindingContext.ModelName];
        var uploadFile = new UploadedFile();
        uploadFile.FileName = file.FileName;
        uploadFile.Content = new byte[(int)file.InputStream.Length];
        file.InputStream.Read(uploadFile.Content, 0, (int)file.InputStream.Length);

        return uploadFile;
   }
}

Global.asax Global.asax中

ModelBinders.Binders[typeof(UploadedFile)] = new UploadedFilesModelBinder();

Controller 调节器

public IHttpActionResult Post(UploadedFile somefile) {
    /* some code */
}

Custom binder never called. 自定义活页夹从未调用过。


I could access the files from the context request form, but the model are quite complex and that would mean I have to bind its properties myself. 我可以从上下文请求表单中访问文件,但是该模型非常复杂,这意味着我必须自己绑定其属性。 I do not want to write a binder if there is already one in the framework. 如果框架中已经有一个活页夹,我不想写一个活页夹。 Is there a way of solving this issue? 有办法解决这个问题吗?

Have a look at this answer https://stackoverflow.com/a/19746825/2138959 . 看看这个答案https://stackoverflow.com/a/19746825/2138959 Also based on Sending HTML Form Data in ASP.NET Web API: File Upload and Multipart MIME you could read multipart form data. 同样基于在ASP.NET Web API中发送HTML表单数据:文件上载和多部分MIME,您可以读取多部分表单数据。 First part that will represent the model you can map to model with media type formatter to avoid manual properties mapping and then read second part with the file and set the file manually to the model. 第一部分将代表模型,您可以使用媒体类型格式化程序将其映射到模型,以避免手动属性映射,然后阅读文件的第二部分并将文件手动设置为模型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM