Javascript 上载图像时,使用SAILJS发布和获取图像不起作用(错误:[object,object])
这是我的代码,参考了sails文档,我想上传一个文件控制器操作的图像,并通过用户控制器操作上传和获取上传和存储。它的表演不起作用Javascript 上载图像时,使用SAILJS发布和获取图像不起作用(错误:[object,object]),javascript,node.js,sails.js,sails-mongo,Javascript,Node.js,Sails.js,Sails Mongo,这是我的代码,参考了sails文档,我想上传一个文件控制器操作的图像,并通过用户控制器操作上传和获取上传和存储。它的表演不起作用 error: [object, Object]. 文件控制器操作 用于上传图像 //myApp/api/controllers/FileController.js module.exports = { index: function (req, res) { res.writeHead(200, { 'content-type': 'te
error: [object, Object].
文件控制器操作
用于上传图像
//myApp/api/controllers/FileController.js
module.exports = {
index: function (req, res) {
res.writeHead(200, {
'content-type': 'text/html'
});
res.end(
//'<form action="http://localhost:1337/file/upload" enctype="multipart/form-data" method="post">'+
'<form action="http://localhost:1337/user/avatar" enctype="multipart/form-data" method="post">' +
'<input type="text" name="title"><br>' +
'<input type="file" name="avatar" multiple="multiple"><br>' +
'<input type="submit" value="Upload">' +
'</form>'
)
},
/**
* User Controller action
* for upload andget image
* Upload avatar for currently logged-in user
*
* (POST /user/avatar)
*/
uploadAvatar: function (req, res) {
console.log('avatar', req.method);
console.log(req.session.me);
req.file('avatar').upload({
// don't allow the total upload size to exceed ~10MB
maxBytes: 10000000
}, function whenDone(err, uploadedFiles) {
if (err) {
return res.negotiate(err);
}
console.log(uploadedFiles);
// If no files were uploaded, respond with an error.
if (uploadedFiles.length === 0) {
return res.badRequest('No file was uploaded');
}
// Save the "fd" and the url where the avatar for a user can be accessed
User.update(req.session.me, {
// Generate a unique URL where the avatar can be downloaded.
avatarUrl: require('util').format('%s/user/avatar/%s', sails.getBaseUrl(), req.session.me),
// Grab the first file and use it's fd (file descriptor)
avatarFd: uploadedFiles[0].fd
})
.exec(function (err) {
if (err) return res.negotiate(err);
return res.ok();
});
});
},
/**
* Download avatar of the user with the specified id
*
* (GET /user/avatar/:id)
*/
avatar: function (req, res) {
console.log('avatar', req.method);
console.log(req.session.me);
req.validate({
//id: 'string'
id: '57053900e15832cc51b84c47'
});
User.findOne(req.param('id')).exec(function (err, user) {
if (err) return res.negotiate(err);
if (!user) return res.notFound();
// User has no avatar image uploaded.
// (should have never have hit this endpoint and used the default image)
if (!user.avatarFd) {
return res.notFound();
}
var SkipperDisk = require('skipper-disk');
var fileAdapter = SkipperDisk(/* optional opts */);
// Stream the file down
fileAdapter.read(user.avatarFd)
.on('error', function (err) {
return res.serverError(err);
}).pipe(res);
});
}
};
module.exports={
索引:功能(req、res){
文书标题(200{
“内容类型”:“文本/html”
});
res.end(
//''+
'' +
“
”+
“
”+
'' +
''
)
},
/**
*用户控制器操作
*用于上传和获取图像
*上载当前登录用户的头像
*
*(帖子/用户/化身)
*/
上传头像:功能(请求、恢复){
控制台日志('avatar',请求方法);
日志(req.session.me);
请求文件('avatar')。上传({
//不允许总上传大小超过~10MB
最大字节数:10000000
},函数whenDone(错误,上载文件){
如果(错误){
返回res.negotiate(err);
}
console.log(上传的文件);
//如果未上载任何文件,请以错误响应。
如果(uploadedFiles.length==0){
return res.badRequest('没有上传文件');
}
//保存“fd”和可以访问用户化身的url
用户更新(req.session.me{
//生成可下载化身的唯一URL。
avatarUrl:require('util').format('%s/user/avatar/%s',sails.getBaseUrl(),req.session.me),
//抓取第一个文件并使用它的fd(文件描述符)
avatarFd:已上载文件[0]。fd
})
.exec(函数(错误){
if(err)返回res.negotiate(err);
返回res.ok();
});
});
},
/**
*下载具有指定id的用户的化身
*
*(GET/user/avatar/:id)
*/
化身:功能(请求、恢复){
控制台日志('avatar',请求方法);
日志(req.session.me);
请求验证({
//id:'字符串'
id:'57053900e15832cc51b84c47'
});
User.findOne(请求参数('id')).exec(函数(err,User){
if(err)返回res.negotiate(err);
如果(!user)返回res.notFound();
//用户没有上传头像图像。
//(本应从未命中此端点并使用默认图像)
如果(!user.avatarFd){
return res.notFound();
}
var SkipperDisk=require('skipper-disk');
var fileAdapter=SkipperDisk(/*可选选项*/);
//将文件向下流
fileAdapter.read(user.avatarFd)
.on('error',函数(err){
返回res.serverError(err);
}).管道(res);
});
}
};
发生错误的行数是多少?并显示详细的错误消息。3到4行的错误我已经张贴了错误图像也。感谢3到4行的错误,我刚刚发布了错误图片。请检查下一行的错误消息。if(err){console.log(err);//检查this.res.negotiate(err);}并检查chrome开发者工具的网络选项卡。