gpt4 book ai didi

node.js - 关于使用Multer(1.0.6)和Express上传文件

转载 作者:搜寻专家 更新时间:2023-10-31 23:32:48 25 4
gpt4 key购买 nike

我想使用 multer 从表单中快速上传图像。

我找到一个 post关于如何使用 multer 上传文件,并触发事件重命名文件。但是,我发现它只适用于旧版本 (multer-0.1.6)

在 multer 的最新指令中 github , 我想我必须使用 disk storage engine 来上传新名称的文件,但是它不起作用。请给我一些说明,我试了一整天这个功能......

此外,我不确定为什么需要 res.status(204).end();?我不添加这行,当我点击上传按钮时网页总是锁定...

package.json

{
"name": "FileUpload",
"version": "0.0.0",
"private": true,
"scripts": {
"start": "node ./bin/www"
},
"dependencies": {
"body-parser": "~1.13.2",
"cookie-parser": "~1.3.5",
"debug": "~2.2.0",
"express": "~4.13.1",
"jade": "~1.11.0",
"morgan": "~1.6.1",
"multer": "^1.0.6",
"serve-favicon": "~2.3.0"
}
}

上传.js

var express = require('express');
var multer = require('multer');
var upload = multer({ storage: storage });

var app = express();

app.post('/uploadImage', upload.single('image'), function (req, res, next) {
console.log("success");
console.log(req.file);
res.status(204).end();
});

app.get('/',function(req,res){
console.log("It's work");
res.sendFile(__dirname + "/index.html");
});

var storage = multer.diskStorage({
destination: function (req, file, cb) {
console.log("Dest");
cb(null, 'uploads/')
},
filename: function (req, file, cb) {
cb(null, file.fieldname + '-' + Date.now())
}
});

app.listen(3000,function(){
console.log("Working on port 3000");
});

index.html

<form id        =  "uploadForm"
enctype = "multipart/form-data"
action = "/uploadImage"
method = "post"
>
<input type="file" name="image" />
<input type="submit" value="Upload Image" name="submit">
</form>

解决方案我的代码无法运行的原因是代码的顺序......以下顺序是正确的。

var express = require('express');
var multer = require('multer');

var app = express();

app.get('/',function(req,res){
console.log("It's work");
res.sendFile(__dirname + "/index.html");
});

var storage = multer.diskStorage({
destination: function (req, file, cb) {
console.log("Dest");
cb(null, 'uploads/')
},
filename: function (req, file, cb) {
cb(null, file.fieldname + '-' + Date.now())
}
});

var upload = multer({ storage: storage });

app.post('/uploadImage', upload.single('image'), function (req, res, next) {
console.log("success");
console.log(req.file);
res.status(204).end();
});

app.listen(3000,function(){
console.log("Working on port 3000");
});

最佳答案

在 GitHub 中你可以找到这个例子,对我来说它是有效的:

var express = require('express');
var multer = require('multer');
var storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, '/tmp/my-uploads')
},
filename: function (req, file, cb) {
cb(null, file.fieldname + '-' + Date.now())
}
})

var upload = multer({ storage: storage })

var app = express();

app.post('/uploadImage', upload.single('image'), function (req, res, next) {
console.log("success");
console.log(req.file);
res.status(204).end();
});

关于node.js - 关于使用Multer(1.0.6)和Express上传文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33186687/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com