2017-09-11 13 views
0

ファイルをアップロードしたいとしましょう。WebコントローラアクションからAPIコントローラアクションにファイル(IFormFile)を送信する方法?

レイザービューで私のHTMLフォームは次のようになります。私は、さらに使用しています

public async Task<IActionResult> Save(TenantBranding tenantBranding) 
    { 
     var result = 
      await _apiClient.PostAsync("/BrandingSettings", tenantBranding); 

     switch (result.Status) 
     { 
      case HttpStatusCode.NotFound: 
      case HttpStatusCode.BadRequest: 
       return new RedirectResult("~/Error/404"); 
      case HttpStatusCode.OK: 
       //return View("Edit", result.Message.Content); 
      default: 
       return new RedirectResult("~/Error/500"); 
     } 
    } 

@model SequereMe.Onboarding.Web.ViewModels.Branding.TenantBrandingViewModel 
@{ 
} 

<form asp-controller="BrandingSettings" asp-action="Save" asp-method="post" enctype="multipart/form-data"> 
    <div class="form-group"> 
     <input asp-for="TenantId" [email protected] /> 
    </div> 
    <div class="form-group"> 
     <label for="logoFileUrl">Logo File Upload:</label> 
     <input asp-for="LogoFile" type="file" class="form-control-file" id="logoFileUrl" aria-describedby="fileHelp"> 
     <h1>@Model.LogoFileUrl</h1> 
    </div> 
    <div class="form-group"> 
     <label for="backgroundFileUrl">Background File Upload:</label> 
     <input asp-for="BackgroundFile" type="file" class="form-control-file" id="backgroundFileUrl" aria-describedby="fileHelp"> 
     <h1>@Model.BackgroundFileUrl</h1> 
    </div> 
    <button type="submit" class="btn btn-primary">Save</button> 
</form> 

このHTMLフォームが提出にヒットするコントローラのアクションは、このウェブコントローラのアクションでありますFluentClientのPathoschild.Http.Client.IClientインターフェイスを使用して、このPostメソッドへのAPI呼び出しを呼び出します。

 [HttpPost] 
     public async Task<IActionResult> Post([FromBody] TenantBranding tenantBranding) 
     { 
      if (tenantBranding == null) 
      { 
       return new BadRequestObjectResult("Invalid Parameter: The incoming tenantBranding parameter is null"); 
      } 

      if (tenantBranding.TenantId == Guid.Empty) 
      { 
       return new BadRequestObjectResult("Invalid Parameter: The tenantId in the incoming parameter tenantBranding is empty"); 
      } 

      var result = _brandingLogic.Save(tenantBranding).Result; 

      if (!result) 
      { 
       return new JsonResult(500); 
      } 
      return Ok(); 
     } 

いくつかの奇妙な理由から、TenantbrandingパラメータはApi Postメソッドではnullです。 ModelBinding fra Web ControllerとApi Controllerの間に問題があります。

これは私のTenantBrandingモデル(API)がどのように見えるかされています

public class TenantBranding 
{ 
     public Guid TenantId { get; set; } 
     public IEnumerable<FormFile> LogoFile { get; set; } 
     public IEnumerable<FormFile> BackgroundFile { get; set; } 
     public string DocumentType { get; set; } 
} 

これは、Webでの私のTenantBrandingがどのように見えるかです:

public class TenantBranding 
{ 
    public Guid TenantId { get; set; } 
    public IFormFile LogoFile { get; set; } 
    public IFormFile BackgroundFile { get; set; } 
} 

しかし、API方式でtenantBrandingパラメータが表示されていますnullなので、なぜこれが起こっているのか分かりません。 IFormFileに関連していますか?

答えて

0

まず、入力に名前属性を追加してみます。

<input asp-for="BackgroundFile" type="file" name="BackgroundFile" class="form-control-file" id="backgroundFileUrl" aria-describedby="fileHelp"> 

しかし、私はあなたのAPIコントローラのための他の方法と使用AJAX要求を行くお勧め:フォームを送信すると

$(form).on("submit", function(){ 
    $.ajax({ 
     url:"api/controller/action", 
     type:"POST", 
     //here will be your data from FORM 
     data:{ 
       TenantId:$("#tenantid input id").val(), 
       LogoFile :$("#LogoFile input id").val(), 
       BackgroundFile :$("#BackgroundFile input id").val(),     
      }, 
     content-type:"application/json", 
     success:function(response){// do on success}, 
     error:function(){// do somesthig on error} 
    }); 
}) 

、JS APIコントローラにPOSTリクエストを送信します。 WebApiを使用するより良い方法です。

関連する問題