使用Ajax将图像发布到Node.js服务器失败

我有这个nodejs express服务器:

const express = require('express');
const app = express();
const port = 3000;
app.listen(port);

并且我需要加注:

const multer  = require('multer');
const upload = multer({ dest: 'files/' });
app.post('/files',upload.any('uploadImage'),function (req,res,next) {
    console.log(req.files);
    // req.file is the file
    // req.body will hold the text fields,if there were any
});

我使用了multer documentation中的确切代码。

这是我的前端代码:

function uploadBrandLogo () {
    const input = $(this).siblings('#brandLogoInput');
    const logo = input[0].files[0];
    const form = new FormData();
    form.append('brandLogo',logo);
    requestController.abortAllPreviousRequests();
    requestController.requests[requestController.requests.length] = $.post({
        url: '/files',data: form,processData: false,// contentType: false,success: uploadBrandLogoSuccess
    });
}

问题是当我尝试发布图像时,我对req.file的定义不确定。 如果取消注释contentType: false行,则会得到:

MulterError: Unexpected field
    at wrappedFileFilter (E:\Admin Panel Node JS Server\admin-panel---project\node_modules\multer\index.js:40:19)
    at Busboy.<anonymous> (E:\Admin Panel Node JS Server\admin-panel---project\node_modules\multer\lib\make-middleware.js:114:7)
    at Busboy.emit (events.js:198:13)
    at Busboy.emit (E:\Admin Panel Node JS Server\admin-panel---project\node_modules\busboy\lib\main.js:38:33)
    at PartStream.<anonymous> (E:\Admin Panel Node JS Server\admin-panel---project\node_modules\busboy\lib\types\multipart.js:213:13)
    at PartStream.emit (events.js:198:13)
    at HeaderParser.<anonymous> (E:\Admin Panel Node JS Server\admin-panel---project\node_modules\dicer\lib\Dicer.js:51:16)
    at HeaderParser.emit (events.js:198:13)
    at HeaderParser._finish (E:\Admin Panel Node JS Server\admin-panel---project\node_modules\dicer\lib\HeaderParser.js:68:8)
    at SBMH.<anonymous> (E:\Admin Panel Node JS Server\admin-panel---project\node_modules\dicer\lib\HeaderParser.js:40:12)

我什至不想存储文件,我只想将其发布到另一个服务,有人可以找出问题吗?欢呼。

fyplsl 回答:使用Ajax将图像发布到Node.js服务器失败

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3161416.html

大家都在问