将多个文件提交到接受ICollection< IFormFile>的ASP.NET控制器. [英] Submitting multiple files to ASP.NET controller accepting an ICollection<IFormFile>

查看:257
本文介绍了将多个文件提交到接受ICollection< IFormFile>的ASP.NET控制器.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的ASP.NET Core后端中,我有一个类似于以下的控制器功能:

In my ASP.NET Core backend, I have a controller function that looks like this:

[HttpPost]
[Route("documents/upload")]
public async Task<IActionResult> UploadFile(ICollection<IFormFile> files)
{
   ...
}

在我的前端,我这样调用函数:

In my front-end, I call the function like this:

var postSettings = {
    method: 'POST',
    credentials: 'include',
    mode: 'cors'
}
uploadDocuments( files ) {
    var data = new FormData();
    data.append('files', files);   
    postSettings.body = data;

    return fetch(endPoint + '/documents/upload', postSettings);
}

如果文件"是单个文件-不是包含一个文件的数组,而是单个File对象-UploadFile会被包含单个文件的ICollection<IFormFile>调用.

If "files" is a single file - not an array with one file, but a single File object - UploadFile is called with an ICollection<IFormFile> containing the single file.

如果文件"是文件列表,即FileList或File对象数组,则使用空的ICollection<IFormFile>调用UploadFile.

If "files" is a list of files, either a FileList or an array of File objects, UploadFile is called with an empty ICollection<IFormFile>.

如何提交文件列表以使它们可以被解析为ICollection<IFormFile>?

How do I submit a list of files in such a way that they can be parsed as an ICollection<IFormFile>?

推荐答案

参考

uploadDocuments(endPoint, files) {
    var postSettings = {
        method: 'POST',
        credentials: 'include',
        mode: 'cors'
    };
    var data = new FormData();
    if(files.length > 1) {
        for(var x = 0; x < files.length; x++) {
            data.append('file' + x, files.item(x));    
        }
    } else {
        data.append('files', files);   
    }
    postSettings.body = data;

    return fetch(endPoint + '/documents/upload', postSettings);
}

参考

使用模型绑定和IFormFile上载文件时 接口,操作方法可以接受单个IFormFileIEnumerable<IFormFile>(或List<IFormFile>)表示 几个文件.以下示例循环遍历一个或多个 上传的文件,将其保存到本地文件系统,然后返回 上传的文件总数和大小.

When uploading files using model binding and the IFormFile interface, the action method can accept either a single IFormFile or an IEnumerable<IFormFile> (or List<IFormFile>) representing several files. The following example loops through one or more uploaded files, saves them to the local file system, and returns the total number and size of files uploaded.

[HttpPost]
[Route("documents/upload")]
public async Task<IActionResult> Post(List<IFormFile> files)
{
    long size = files.Sum(f => f.Length);

    // full path to file in temp location
    var filePath = Path.GetTempFileName();

    foreach (var formFile in files)
    {
        if (formFile.Length > 0)
        {
            using (var stream = new FileStream(filePath, FileMode.Create))
            {
                await formFile.CopyToAsync(stream);
            }
        }
    }

    // process uploaded files
    // Don't rely on or trust the FileName property without validation.

    return Ok(new { count = files.Count, size, filePath});
}

这篇关于将多个文件提交到接受ICollection&lt; IFormFile&gt;的ASP.NET控制器.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆