向接受 ICollection<IFormFile> 的 ASP.NET 控制器提交多个文件
Submitting multiple files to ASP.NET controller accepting an ICollection<IFormFile>
在我的 ASP.NET 核心后端中,我有一个如下所示的控制器函数:
[HttpPost]
[Route("documents/upload")]
public async Task<IActionResult> UploadFile(ICollection<IFormFile> files)
{
...
}
在我的前端,我这样调用函数:
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);
}
如果 "files" 是单个文件 - 不是包含一个文件的数组,而是单个文件对象 - UploadFile
将使用包含单个文件的 ICollection<IFormFile>
调用。
如果 "files" 是一个文件列表,一个 FileList 或一个 File 对象数组,则调用 UploadFile
时带有一个空的 ICollection<IFormFile>
。
如何提交文件列表以使其能够被解析为 ICollection<IFormFile>
?
引用Uploading multiple files at once - with Fetch
uploadDocuments(endPoint, files) {
var postSettings = {
method: 'POST',
credentials: 'include',
mode: 'cors'
};
var data = new FormData();
if(files.length > 0) {
for(var x = 0; x < files.length; x++) {
// the name has to be 'files' so that .NET could properly bind it
data.append('files', files.item(x));
}
}
postSettings.body = data;
return fetch(endPoint + '/documents/upload', postSettings);
}
引用Uploading small files with model binding
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});
}
在我的 ASP.NET 核心后端中,我有一个如下所示的控制器函数:
[HttpPost]
[Route("documents/upload")]
public async Task<IActionResult> UploadFile(ICollection<IFormFile> files)
{
...
}
在我的前端,我这样调用函数:
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);
}
如果 "files" 是单个文件 - 不是包含一个文件的数组,而是单个文件对象 - UploadFile
将使用包含单个文件的 ICollection<IFormFile>
调用。
如果 "files" 是一个文件列表,一个 FileList 或一个 File 对象数组,则调用 UploadFile
时带有一个空的 ICollection<IFormFile>
。
如何提交文件列表以使其能够被解析为 ICollection<IFormFile>
?
引用Uploading multiple files at once - with Fetch
uploadDocuments(endPoint, files) {
var postSettings = {
method: 'POST',
credentials: 'include',
mode: 'cors'
};
var data = new FormData();
if(files.length > 0) {
for(var x = 0; x < files.length; x++) {
// the name has to be 'files' so that .NET could properly bind it
data.append('files', files.item(x));
}
}
postSettings.body = data;
return fetch(endPoint + '/documents/upload', postSettings);
}
引用Uploading small files with model binding
When uploading files using model binding and the
IFormFile
interface, the action method can accept either a singleIFormFile
or anIEnumerable<IFormFile>
(orList<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});
}