简体   繁体   中英

Send IFormFile To API using HttpClient Alwyes Null ASP.net Core2.2

i created API for attachments it should receive model info with files if i send the files the model is null if i did not send files the model data show

Model

public class AttachmentFilesViewModel
{
    public long Id { get; set; }
    public string FileName { get; set; }

    public string FileExt { get; set; }
    public IFormFile files { get; set; }
    public AttachmentViewModel AttachmentViewModel {get;set; }

}

Model 2

 public class AttachmentViewModel
{
    public long Id { get; set; }
    public string Type { get; set; }


    public long TypeID { get; set; }
    public string FileDesc { get; set; }
    public List<AttachmentFilesViewModel> attachmentFiles {get;set; }

}

controller get files from view correct here i build my Viewmodel from the files the files is correct

  [HttpPost]
    public async Task<IActionResult> Create(AttachmentViewModel attachment, IFormFile[] Files)
    {

        LawyerAPI lawyerAPI = new LawyerAPI();
        HttpClient httpClient = lawyerAPI.InitializeClient();
        if (Files != null && Files.Count() > 0)
        {
            attachment.attachmentFiles = new List<AttachmentFilesViewModel>();
            foreach (var item in Files)
            {
                AttachmentFilesViewModel att = new AttachmentFilesViewModel();


                att.FileExt = Path.GetExtension(item.FileName);
                att.FileName = item.FileName;
                att.files = item;
                attachment.attachmentFiles.Add(att);
            }

        }

        HttpResponseMessage res = await httpClient.PostAsJsonAsync("api/nofactory/createattachment", attachment);
        List<AttachmentViewModel> model = new List<AttachmentViewModel>();
        model.Add(attachment);
        return View("index", model);

    }

i also tried to manually serialize

API here i receive null if i add the file to the model if i comment the above foreach and the file list is empty i receive the correct model i also tried [FromBody]

  [Route("createattachment")]
    // POST api/values
    public IActionResult Post([FromForm] AttachmentViewModel attachment)
    {}

how to get the files to the API Thanks

Edit

View Code

  @using (Html.BeginForm(null, null, FormMethod.Post, new { @id ="frmAddAttachment" ,enctype="multipart/form-data"})){@Html.HiddenFor(model => model.Type)@Html.HiddenFor(model => model.TypeID)<div class="row">
<div class="card">       
    <div class="card-content">
        <div class="form-horizontal">
            <div class="col-md-10 col-sm-12 col-xs-12">
                <div class="form-group">
                    @Html.TextBoxFor(b => b.FileDesc})
                </div>
            </div>
            <div class="col-md-10 col-sm-12 col-xs-12">
                <input type="file" name="Files" multiple="multiple" />                   
                <button type="submit" class="btn btn-success"> Save</button>
            </div>
        </div>           
    </div>
</div>

I think the view is OK i do get the files in the controller but the problem in sending them to the API

Use MultipartFormDataContent to send files via HttpClient

[HttpPost]
public async Task<IActionResult> Create([FromForm]AttachmentViewModel attachment, [FromForm]IFormFile[] Files)
{
    //initialize attachmentFiles 

    var content = new MultipartFormDataContent();
    content.Add(new StringContent(attachment.Id.ToString()), "Id");
    content.Add(new StringContent(attachment.Type), "Type");
    content.Add(new StringContent(attachment.TypeID.ToString()), "TypeID");
    content.Add(new StringContent(attachment.FileDesc), "FileDesc");
    for (int i = 0; i < attachment.AttachmentFiles.Count; i++)
    {
        var attachmentFile = attachment.AttachmentFiles[i];

        content.Add(new StreamContent(attachmentFile.Files.OpenReadStream()), $"AttachmentFiles[{i}].Files", attachmentFile.Files.FileName);
        content.Add(new StringContent(attachmentFile.FileExt), $"AttachmentFiles[{i}].FileExt");
        content.Add(new StringContent(attachmentFile.FileName), $"AttachmentFiles[{i}].FileName");
        content.Add(new StringContent(attachmentFile.Id.ToString()), $"AttachmentFiles[{i}].Id");
    }


    HttpResponseMessage res = await httpClient.PostAsync("api/nofactory/createattachment", content);
    List<AttachmentViewModel> model = new List<AttachmentViewModel>();
    model.Add(attachment);
    return View("index", model);

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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