简体   繁体   English

使用HttpClient Alwyes Null ASP.net Core2.2将IFormFile发送到API

[英]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 我为附件创建了API,如果我发送文件,则模型应该显示为null;如果我不发送文件,则模型数据显示为null

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 模型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 控制器从正确的视图中获取文件,这里我从文件正确的构建我的Viewmodel

  [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] 如果我在上面的foreach中添加了注释,并且我将文件添加到模型中,并且文件列表为空,那么我收到的API为空,我也尝试过[FromBody]

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

how to get the files to the API Thanks 如何将文件获取到API谢谢

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 我认为视图还可以,我确实在控制器中获取了文件,但是将它们发送到API时出现了问题

Use MultipartFormDataContent to send files via HttpClient 使用MultipartFormDataContent通过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);

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在ASP.Net Core2.2中覆盖415响应 - How to Override 415 response in ASP.Net Core2.2 使用 ASP.NET Core2.2 中的 CancellationToken 取消内部任务 - Cancel an internal task using CancellationToken in ASP.NET Core2.2 无法使用 C# 客户端将 IFormFile 发送到 ASP.Net Core Web API - unable to send IFormFile to ASP.Net Core Web API using C# client IFormFile 在 asp.net core 2.1 中总是返回 null - IFormFile always return null in asp.net core 2.1 IFormFile 在 ASP.NET Core 3.0 MVC 中始终为 NULL - IFormFile is always NULL in ASP.NET Core 3.0 MVC IFormFile 在 ASP.NET Core 3.0 中返回 NULL - IFormFile is returning NULL in ASP.NET Core 3.0 IFormFile 始终为空(带有 MVC/Razor 的 ASP.NET Core) - IFormFile always null (ASP.NET Core with MVC/Razor) 无法在ASP.Net Core 2中使用IFormFile对象上载具有空引用接收的照片 - Could not upload photo using IFormFile object in ASP.Net Core 2 with null reference expception 创建模型时不能使用上下文。 EF-核心ASP.net Core2.2 - The context cannot be used while the model is being created. EF-Core ASP.net Core2.2 无法使用 Firefox 调试某些 asp.net core2.2 网页。 如何使用 Firefox 进行调试? - Unable to debug some asp.net core2.2 web pages with Firefox. How can I debug with Firefox?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM