0

在 api IFormFile 中收到 null。

Javascript:

var archivo = _('inputCargar').files[0];        
const form = new FormData();
form.append("files", archivo);
const config = {
headers: {
'content-type': 'multipart/form-data'}}     
    await axios.post(`${data.urlUtil}Insert_FileAzure`, form, config);

表单文件到达控制器却无法到达前面的api控制器:

   public async Task<JsonResult> Insert_FileAzure([FromForm]  ICollection<IFormFile> files)
        {
           
            var result = await _apiUtil.PostAsync<ICollection<IFormFile>> ("File/UploadAzure", files);
            return Json(result);
        }

接口:

[HttpPost]
public async Task<ActionResult<RespuestaUpload>> UploadAzure([FromForm(Name = "files")] ICollection<IFormFile> files)
{
  if (files == null || files.Count == 0)
    return Content("file not selected");
  var respuestaUpload = await _utiles.UploadFilesAzure(files);
  return Ok(respuestaUpload);
}
4

1 回答 1

1

用作httpContent MultipartFormDataContent,如下所示:

var Client = new HttpClient();
var multipartFormDataContent = new MultipartFormDataContent();
foreach (IFormFile file in files)
{
    byte[] fileData;
    using (var reader = new BinaryReader(file.OpenReadStream()))
    {
        fileData = reader.ReadBytes((int)file.OpenReadStream().Length);
    }
    var fileContent = new ByteArrayContent(fileData);
    multipartFormDataContent.Add(fileContent, "files", file.FileName);
}

var response = await Client.PostAsync(requestUrl, multipartFormDataContent);
于 2021-02-26T03:00:15.953 回答