1

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

2
  • What does the view look like? Commented Mar 13, 2019 at 10:27
  • I have added the code of the view but i do get the files from the view correct but i can't send them to the API Commented Mar 13, 2019 at 10:39

1 Answer 1

6

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);

}
Sign up to request clarification or add additional context in comments.

3 Comments

IOException: Unable to read data from the transport connection: The I/O operation has been aborted because of either a thread exit or an application request. on the line HttpResponseMessage res = await httpClient.PostAsync("api/nofactory/createattachment", content); if i passed attachment instead of content it work but still null
@MaherKhalil Hard to say what's wrong. I tested this code and it worked fine. Try pass no files, 1 file and multiple files and see what happens
@MaherKhalil the name of your file must be the same name of your Post method parameter, in your case the name must be: attachment. I had the same problem and looking for a solution I found this great tip: makolyte.com/aspnetcore-receive-a-file-in-the-request. Probably you found some solution, but it can be useful to someone else

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.