我如何使用 Javascript 生成input:file
多个但从不同的输入文件中获取文件,让我解释一下:
<inpu type="file" name="file1">
<inpu type="file" name="file2">
<inpu type="file" name="file3">
要使用 ajax 使用 Javascript 发送它们,您应该执行以下操作:
fd = new FormData();
xhr = new XMLHttpRequest();
fd.append('file1', file1, file1.name);
fd.append('file2', file2, file2.name);
fd.append('file3', file3, file3.name);
xhr.open('post', url, true);
xhr.send(fd);
我需要的是(因为我的应用程序input:file
动态创建不同的字段并且我不必更改该功能)是使用类似于此的代码:
fd.append('archivos', files, "archivos");
其中“文件”是一个文件数组。
为什么一定要这样?
因为在 Node 中我使用 Multer 并上传多个文件,所以我必须使用如下中间件:
api.post('/equipos/uploadfiles', upload.array('archivos', 12), equipoController.uploadFiles);
数据:我正在使用 Angular 并且我已经准备好文件上传,但要一个一个地上传。
我的问题的解决方案非常简单,我与您分享:
我已将所有文件添加到数组中,然后按如下方式对其进行迭代:
我希望它可以帮助某人。