将图像上传到节点.js图像“未定义
Upload image to Node.js image' of undefined
对不起,缩进很差我是节点新手.js尝试将图像上传到节点.js检查很多答案,如果我错了,请责备我
1.bodyParser 需要一个中间件来处理二进制文件,比如 IMAGE如果我不使用它,它会显示
undefine token
2.使用Multer作为中间件,像这样
var multer = require('multer') var upload = multer({ dest:'/Node/file-upload/uploads/' }); app.post('/upload',upload.array(),songs.upload);
- 按照这个,req.files是图像文件,req.body是文件名所以我使用 fs.readFile 来读取文件
fs.readFile(req.files.image.path, function (err, data)
4.比我需要文件路径和文件名,我使用req.body
var dirname = "/Node/file-upload/uploads/"; var newPath = dirname + req.body.image.filename;
5.通过此将数据写入目标
fs.writeFile(newPath, data, function (err)
我不确定我错过了哪一部分,我花了 10 个小时才做到这一点,请帮忙
它总是告诉我类型错误:无法读取未定义的属性"图像"
应用.js
var express = require('express')
,bodyParser = require('body-parser')
,app = express()
,multer = require('multer')
,binary = require('binary')
,fs = require('fs')
,util= require('util')
,http = require('http')
,multer = require('multer')
,upload = multer({ dest: '/Node/file-upload/uploads/' });
app.use(bodyParser.urlencoded({ extended: true })); // support encoded bodies.
app.use(bodyParser.json({limit: '5mb'}));
songs = require('./routes/route');
app.listen(3000, function () {
console.log('Example app listening on port 3000!');
});
app.post('/upload',upload.array(),songs.upload);
路线.js
var mongoose = require('mongoose');
var uri = "mongodb://xxxxxx:xxxxxxx@ds061365.mongolab.com:61365/aweitest";
mongoose.connect(uri);
// we're connected!
var db = mongoose.connection.db;
var BSON = require('bson').BSONPure;
var binary = require('binary');
var body = require('body-parser');
var fs = require('fs');
db.on('error', console.error.bind(console, 'connection errrrrrrrror:'));
db.once('open', function() {
console.log("mongodb is connected!!");
});
exports.upload = function(req, res) {
console.log(req.body);
fs.readFile(req.files.image.path, function (err, data){
var dirname = "/Node/file-upload/uploads/";
var newPath = dirname + req.body.image.filename;
fs.writeFile(newPath, data, function (err) {
if(err){
res.json({'response':"Error"});
}else {
res.json({'response':"Saved"});
}
});
});
};
错误
TypeError: Cannot read property 'image' of undefined
at exports.upload (c:'Users'awei'WebstormProjects'untitled'routes'girlshanlder.js:92:26)
at Layer.handle [as handle_request] (c:'Users'awei'node_modules'express'lib'router'layer.js:95:5)
at next (c:'Users'awei'node_modules'express'lib'router'route.js:131:13)
at multerMiddleware (c:'node_modules'multer'lib'make-middleware.js:18:41)
at Layer.handle [as handle_request] (c:'Users'awei'node_modules'express'lib'router'layer.js:95:5)
at next (c:'Users'awei'node_modules'express'lib'router'route.js:131:13)
at Route.dispatch (c:'Users'awei'node_modules'express'lib'router'route.js:112:3)
at Layer.handle [as handle_request] (c:'Users'awei'node_modules'express'lib'router'layer.js:95:5)
at c:'Users'awei'node_modules'express'lib'router'index.js:277:22
at Function.process_params (c:'Users'awei'node_modules'express'lib'router'index.js:330:12)
at next (c:'Users'awei'node_modules'express'lib'router'index.js:271:10)
at jsonParser (c:'Users'awei'node_modules'body-parser'lib'types'json.js:107:37)
at Layer.handle [as handle_request] (c:'Users'awei'node_modules'express'lib'router'layer.js:95:5)
at trim_prefix (c:'Users'awei'node_modules'express'lib'router'index.js:312:13)
at c:'Users'awei'node_modules'express'lib'router'index.js:280:7
at Function.process_params (c:'Users'awei'node_modules'express'lib'router'index.js:330:12)
app.js
中的upload.array()
应该是
upload.array('image',1) // 1 here is number of files you would upload
在您的songs.upload
中间件中,您将获得 req.files.image 作为一个数组,
因此,您最好检查数组的长度并迭代它以保存每个文件,或者如果您只想保存 1 个文件,您可以执行以下操作。
例:
应用.js
app.post('/upload',upload.array('image',1),songs.upload);
路线.js
var mongoose = require('mongoose');
var uri = "mongodb://xxxxxx:xxxxxxx@ds061365.mongolab.com:61365/aweitest";
mongoose.connect(uri);
// we're connected!
var db = mongoose.connection.db;
var BSON = require('bson').BSONPure;
var binary = require('binary');
var body = require('body-parser');
var fs = require('fs');
db.on('error', console.error.bind(console, 'connection errrrrrrrror:'));
db.once('open', function() {
console.log("mongodb is connected!!");
});
exports.upload = function(req, res) {
console.log(req.body);
fs.readFile(req.files.image[0].path, function (err, data){
var dirname = "/Node/file-upload/uploads/";
var newPath = dirname + req.body.image.filename;
fs.writeFile(newPath, data, function (err) {
if(err){
res.json({'response':"Error"});
}else {
res.json({'response':"Saved"});
}
});
});
};
相关文章:
- 未捕获的类型错误: 无法读取未定义的属性“图像”
- Javascript图像宽度和高度未定义值
- 隐藏或删除图像 src=未定义
- SimpleCart JS Cart图像未定义
- 节点.js 使用 Multer 上传图像显示未定义
- ERB/Coffee Rails 中图像网址的未定义局部变量或方法“image”
- “未捕获的类型错误:无法读取未定义的属性'嵌入'”在 pdfMake pdf 引擎中使用自定义图像时
- 带有PHP(未定义索引)的Youtube API V3 GET横幅图像
- 使图像库未定义不是一个函数
- 将图像上传到节点.js图像“未定义
- 传单图像 - 未捕获类型错误:无法读取未定义的属性“匹配”
- 图像名称返回为未定义,但仅适用于某些图像
- 华丽的弹出窗口:获得"无法加载图像“;并且图像url未定义
- jQuery文件上传'未定义'图像url
- javascript随机图像;未定义”;
- JSON类型错误:无法读取属性'图像'的未定义
- 图像按钮串联变量给出未定义的错误
- 图像激发onload事件,但图像仍然未定义
- 后续:预加载图像会引发未定义的错误
- JQuery警告通过ajax加载的图像src.未定义的错误