如何使用multer在node.js服务器中上传文件
我正在尝试将文件从Angular应用程序传递到node.js服务器 当我运行应用程序时,出现以下错误: 错误:请选择文件 HTML: 节点: 在另一个项目中,multer按预期工作。以下是该项目的HTML:如何使用multer在node.js服务器中上传文件,node.js,angular,multer,Node.js,Angular,Multer,我正在尝试将文件从Angular应用程序传递到node.js服务器 当我运行应用程序时,出现以下错误: 错误:请选择文件 HTML: 节点: 在另一个项目中,multer按预期工作。以下是该项目的HTML: <form action="/uploadmultiple" enctype="multipart/form-data" method="POST"> Select images: <input type="file" name="myFiles" multiple
<form action="/uploadmultiple" enctype="multipart/form-data" method="POST">
Select images: <input type="file" name="myFiles" multiple>
<input type="submit" value="Upload your files" />
</form>
我的工作代码与不工作的代码之间的区别在于,如果类型为file,我可以使用标准输入控件。
但是我现在需要使用一个上传控件&当我做一个更改时,我的代码不起作用
有人能告诉我如何使用此控件传递文件吗?提前多谢 使用npm安装安装multer后-保存multer 基本用法示例:
var express = require('express')
var multer = require('multer')
var upload = multer({ dest: 'uploads/' })
var app = express()
app.post('/uploadmultiple', upload.single('myFiles'), function (req, res, next) {
// req.file is the `myFiles ` file
// req.body will hold the text fields, if there were any
})
app.post('/uploadmultiple', upload.array('myFiles', 12), function (req, res, next) {
// req.files is array of `photos` files
// req.body will contain the text fields, if there were any
})
有关更多信息,请阅读文档您好,谢谢您的回答。实际上,我让multer在做一个较小的项目,就像你在这里展示的一样。但在我现在的项目中,我需要使用来自第三方的上传控件,而不是标准的输入控件。之所以我的代码无法工作,是因为node.js中的upload.array方法需要HTML中的控件名吗?我已经用name属性更新了上面的代码,但仍然收到相同的错误消息。upload组件是否在onChange事件中提供formData,那么你的工作就可以轻松完成了。我已经在上面添加了我的updateList方法代码。当使用上载控件上载文件时,将执行此操作。执行此操作时,会将其打印到控制台:demo list-[{file:{},id:0,icon:doc,src:{changingThisBreaksApplicationSecurity:data:application/vnd.openxmlformats officedocument.wordprocessingml.document;base64,}]send.component.ts:50:4文件-[{file:{},id:0,icon:doc,src:{更改此突破性应用程序安全性:数据:application/vnd.openxmlformats of cedocument.wordprocessingml.document;base64,}]
const express = require('express')
const app = express()
const bodyParser = require('body-parser')
const multer = require('multer');
let nodemailer = require('nodemailer');
let aws = require('aws-sdk');
const fs = require('fs');
var storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, 'uploads')
},
filename: function (req, file, cb) {
cb(null, file.fieldname + '-' + Date.now() + path.extname(file.originalname))
}
});
var upload = multer({ storage: storage });
app.post('/postData', upload.array('fileUpload', 12), (req, res, next) => {
console.log(req.body);
res.json(req.body);
const files = req.files
if (!files) {
const error = new Error('Please choose files')
error.httpStatusCode = 400
return next(error)
}
res.send(files);
}
<form action="/uploadmultiple" enctype="multipart/form-data" method="POST">
Select images: <input type="file" name="myFiles" multiple>
<input type="submit" value="Upload your files" />
</form>
var express = require('express')
var multer = require('multer')
var upload = multer({ dest: 'uploads/' })
var app = express()
app.post('/uploadmultiple', upload.single('myFiles'), function (req, res, next) {
// req.file is the `myFiles ` file
// req.body will hold the text fields, if there were any
})
app.post('/uploadmultiple', upload.array('myFiles', 12), function (req, res, next) {
// req.files is array of `photos` files
// req.body will contain the text fields, if there were any
})