上传文件在 ApiController c# 中不起作用

Upload file doesn't work in ApiController c#

我有一个 C# 应用程序,其中包含用于上传图像的表单,但调用转到了控制器,我无法获取上传文件。 Request.Content.IsMimeMultipartContent() 是假的,file.Count() = 0file2 是一个字符串。

@model Form
<div class="form-wrapper">
    <h3>@Model.Title</h3>
    @if (!string.IsNullOrEmpty(Model.Intro))
    {
        <p>@Html.Raw(Model.Intro)</p>
    }

    <form class="form"
          enctype="multipart/form-data"
          novalidate
          data-required-label="@Html.GetLabel("form-required-field")"
          data-required-group-label="@Html.GetLabel("form-choice-makechoice")"
          data-api-endpoint="/api/form">

        <input id="test" name="test" type="file" required/>
        <input type="hidden" name="tcm" readonly value="@Model.TcmUri">
        <p class="form-section form-section--submit">
            <button class="btn-primary--with-icon-internal" type="submit">@Html.GetLabel("form-submit-button")</button>
        </p>
    </form>
</div>


[System.Web.Http.RoutePrefix("api/form")]
public class ApiFormController : ApiController
{
    public ApiFormController()
    {
    }

    [System.Web.Http.Route("")]
    [System.Web.Http.HttpPost]
    public async Task<HttpResponseMessage> Form()
    {
        if (Request.Content.IsMimeMultipartContent())
            throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);

        var file = HttpContext.Current.Request.Files;
        var requestForm = HttpContext.Current.Request.Form;
        var file2 = requestForm["test"];
    }
}

此代码 enctype="multipart/form-data" 在用于此 if (Request.Content.IsMimeMultipartContent()) 的表单标记中。还是清洗一下比较好。

我修改了代码。首先,不要改变形式。如果答案无效,请使用下面表格的定义

步骤 1

创建ApiFormController.cs

public class ApiFormController : ApiController
{
    public ApiFormController()
    {
    }

    [System.Web.Http.HttpPost]
    public async Task<HttpResponseMessage> Form()
    {
       var files = HttpContext.Current.Request.Files;

       //one file
       HttpPostedFile postedfile = files.Get("test");

       //multi file
       List<HttpPostedFile> postedfiles = new List<HttpPostedFile>();
       foreach (var item in files)
       {
           postedfiles.Add(files.Get(item.ToString()));
       }
       
       //save file
       postedfile.SaveAs(Path.Combine("~/uploadfiles/", postedfile.FileName));
       return new HttpResponseMessage(HttpStatusCode.OK);
    }
}

步骤 2

创建WebApiConfig.cs

public static class WebApiConfig
{
   public static void Register(HttpConfiguration config)
   {
     config.MapHttpAttributeRoutes();

     config.Routes.MapHttpRoute(
        name: "DefaultApi",
        routeTemplate: "api/form",
        defaults: new { controller = "ApiForm", action = "Form" }
      );
   }
}

步骤 3

在global.cs

中注册WebApiConfig class
protected void Application_Start()
{
     //...........
     GlobalConfiguration.Configure(WebApiConfig.Register);
     //..........
}

步骤 4

创建视图

<form class="form"
         enctype="multipart/form-data" action="/api/form" method="post">

      <input id="test" name="test" type="file" />
      <br />
      <input type="file" name="test1" id="test1" />
      <p class="form-section form-section--submit">
          <button class="btn-primary--with-icon-internal" type="submit">Upload Files</button>
      </p>
</form>