本文共 618 字,大约阅读时间需要 2 分钟。
使用JavaScript实现文件上传的方法如下:
var data = new FormData();// 遍历所有文件输入域$.each($("input[type='file']")[0].files, function(i, file) { data.append('file', file);}); 通过以上方法,可以将文件数组提交到服务器:
$.ajax({ type: 'POST', url: '/your/url', cache: false, contentType: false, processData: false, data: data, success: function(result) { console.log(result); }, error: function(err) { console.log(err); }}); 如果多次调用data.append('file', file),服务器会接收到文件数组。例如,使用Node.js和multer中间件:
router.post('/trip/save', upload.array('file', 10), function(req, res) { // 你的文件数组现在在`req.files`中}); 转载地址:http://tvtfk.baihongyu.com/