lundi 29 mars 2021

Could not post value to web API using IFormFile

i have a web application that does an action to upload image to api the code like this

[HttpPost]
public async Task<IActionResult> Upload([FromForm] UploadModel model)
{
    var upload = AccountApi.UploadFile(model);
    return Ok("OK");
}

public static object UploadFile(UploadModel model)
{
    RestClient client = InitClient();
    request.Resource = "Account/UploadFile";
    request.Method = Method.POST;
    request.AddJsonBody(model);
    IRestResponse<object> response = client.Execute<object>(request);
    return response.Data;
}

public class UploadModel
{
    public long UserId { get; set; }
    public string Token { get; set; }
    public IFromFile File { get; set; }
}

and there's a web API to handle Rest request above the code like this

[HttpPost("UploadFile")]
public async Task<object> UploadFileAction(UploadModel model)
{
    // the code handle upload file request here
    return "Success";
}

my issue is the UploadModel model in web application contains the right value that requested from front-end (UserId = 10, Token = "eyJ..........", File = [object]) but when it posted to API, the 3 properties in UploadModel didn't get the value posted from web application (UserId = 0, Token = null, File = null)

could you help me to find the solution for this. Thanks all




Aucun commentaire:

Enregistrer un commentaire