javascript - 使用 XMLHttpRequest 将多个文件上传到 Express.js 3.5 服务器

标签 javascript node.js file-upload express xmlhttprequest

我正在尝试使用 JavaScript 中的 native FileAPI 构建文件 uploader ,我想通过 XMLHttpRequest(不使用 jQuery)将文件上传到使用 Express.js 的 Node.js 服务器。

文件读取部分工作正常,当我在没有 XMLHttpRequest 的情况下上传文件时,它工作正常(文件在 Express.js 的 req.files 中)。

问题是通过 AJAX 上传:req.files 总是空的。

下面是一些代码:

形式:

<form action="http://localhost:3000/upload" method="POST" enctype="multipart/form-data" name="form">
  <input type="file" name="uploads" id="files" multiple="multiple">
  <input type="submit" name="submit" value="submit">
</form>

前端的上传部分(在 files[0].data 中是一个文件 - 不是数组或其他东西):

function uploadFiles(files) {
    var xhr = new XMLHttpRequest();
    xhr.submittedData = files; // Array of objects with files included. But it neither works with an array of files nor just one file
    xhr.onload = successfullyUploaded;
    xhr.open("POST", "http://localhost:3000/upload", true);
    xhr.setRequestHeader('X-Requested-With','XMLHttpRequest');
    xhr.send();
}

出现问题的后端:

exports.receiveUpload = function(req, res){
    console.log(req.files); // empty
    var files = req.files.uploads; // always empty with AJAX upload. with normal upload it's fine
    console.log(req.xhr); // true
    // ...
}

这里是一些 Express.js 配置(我在没有 AJAX 的情况下已经遇到了同样的错误 - 在代码的注释中,您可以看到这些行和 Stack Overflow 帖子解决了在没有 AJAX 的情况下上传的问题):

// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.json());
app.use(express.urlencoded());

// this 3 lines have to be before app.use(app.router)
// https://stackoverflow.com/questions/21877098/upload-file-using-express-failed-cannot-read-property-of-undefined
app.use(express.multipart());
app.use(express.bodyParser({ keepExtensions: true, uploadDir: path.join(__dirname, 'public', 'uploads') }));
app.use(express.methodOverride());


app.use(app.router);
app.use(require('less-middleware')(path.join(__dirname, '/public')));
app.use(express.static(path.join(__dirname, 'public')));

提前致谢!

问候,

C.

最佳答案

感谢@Pengtuzi 我解决了它:

我使用 FormData API 上传文件。我的错误是我认为错误会发生在服务器上。

这是为我解决问题的代码:

function uploadFiles(files) {
    var xhr = new XMLHttpRequest();
    var formData = new FormData();
    xhr.onload = successfullyUploaded;
    xhr.open("POST", "http://localhost:3000/upload", true);
    xhr.setRequestHeader('X-Requested-With','XMLHttpRequest');
    for(var file in files) {
        formData.append("uploads", files[file].data);
    }
    xhr.send(formData);
}

关于javascript - 使用 XMLHttpRequest 将多个文件上传到 Express.js 3.5 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23035977/

相关文章:

javascript - 警报返回为未定义

php - 简单的 php .. 不工作......嗯看起来很简单

Node.js:Winston:我可以将默认元数据添加到所有日志消息中吗

python - 使用 AngularJS 上传文件失败

javascript - 创建JS : Usage of "containers"

javascript - 使用 CoAP 向 CoRE 资源目录发送 PUT 请求

javascript - 无法使用 puppeteer 从图像列表中获取 url

node.js - 是否可以在supertest的错误信息中添加信息

java - 使用 Jersey 客户端上传文件的最佳方法是什么?

c# - 如何在ASP.Net上传控件中指定文件扩展名?