webapi 上传文件和数据

I had the same problem. Solved by MultipartDataMediaFormatter for ASP.NET WebApi. How to use:

  1. Find and install from Nuget packages MultipartDataMediaFormatter.
  2. Add current formatter to WebApi formatters collection:

    • if WebApi hosted on IIS (on Application Start). :
      • GlobalConfiguration.Configuration.Formatters.Add(new FormMultipartEncodedMediaTypeFormatter());
    • if WebApi is self-hosted:

      • new HttpSelfHostConfiguration(<url>).Formatters.Add(new FormMultipartEncodedMediaTypeFormatter());

After that you can post objec with file together in one model.

Note: In Nuget packages version 1.1.0 is not the last. They are not updated yet. Maybe possible to install latest version manually.

猜你喜欢

转载自blog.csdn.net/LU142857/article/details/84982335