簡體   English   中英

使用HttpClient Alwyes Null ASP.net Core2.2將IFormFile發送到API

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

我為附件創建了API,如果我發送文件,則模型應該顯示為null;如果我不發送文件,則模型數據顯示為null

模型

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

}

模型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; }

}

控制器從正確的視圖中獲取文件,這里我從文件正確的構建我的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);

    }

我也嘗試過手動序列化

如果我在上面的foreach中添加了注釋,並且我將文件添加到模型中,並且文件列表為空,那么我收到的API為空,我也嘗試過[FromBody]

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

如何將文件獲取到API謝謝

編輯

查看代碼

  @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>

我認為視圖還可以,我確實在控制器中獲取了文件,但是將它們發送到API時出現了問題

使用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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM