上传目录不会发送带有文件的文件夹名称

我尝试了目录上传的新functionhttps://stackoverflow.com/a/8218074/2004910,但我没有收到确切的文件夹结构的服务器请求。

HTML

<form action="http://localhost:3000/" method="post" enctype="multipart/form-data"> <input id="files" class="file" type="file" name="file[]" webkitdirectory directory> <input type="submit" /> 

请求负载(networking面板)

在这里输入图像说明

ExpressJS:

 const express = require('express') const app = express() var busboy = require('connect-busboy'); var fs = require('fs'); //... app.use(busboy()); app.post('/', function (req, res) { var fstream; req.pipe(req.busboy); req.busboy.on('file', function (fieldname, file, filename) { console.log("Uploading: " + filename); fstream = fs.createWriteStream(__dirname + '/files/' + filename); file.pipe(fstream); fstream.on('close', function () { res.redirect('back'); }); }); }) app.listen(3000, function () { console.log('Example app listening on port 3000!') }) 

请告诉我如何做到这一点。

来自https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement/webkit目&#x5F55;

用户进行select后,文件中的每个File对象都将其File.webkitRelativePath属性设置为所选文件所在目录中的相对path。

我不认为这是通过POST请求艰难,你将不得不在我认为的客户端工作。

 $('#files').on('change',function(e){ var files = e.target.files; for (let i=0; i<files.length; i++) { var path = files[i].webkitRelativePath; $('#form').append('<input type="hidden" name="filepath[]" value="'+path+'"/>'); } }); 

这个脚本会附加隐藏的input,用POST发送文件path。 您的脚本可以使用它。 如果我向你发送了一个文件名相同的文件,但是在两个不同的文件夹中,你可能不会把正确的文件放在正确的文件夹中。