SailsJS API 文件上传
SailsJS API file upload
我正在使用我找到的教程 here to help me figure out file uploads in SailsJS. I have been able to get the uploads working when using the .ejs template engine in Sails, but I need the file uploads to work with the RESTapi. The URL I have setup is 'http://localhost:1337/file/upload',我正在使用 Postman chrome 应用程序将文件发送到服务器,但我得到的响应是:
{
"status": 200,
"file": []
}
当不使用 API(在 .ejs 模板中执行)时,我得到以下响应:
{
"status": 200,
"file": [
{
"fd": "path/to/uploaded/file/.tmp/uploads/assets/images/18c60ef7-b176-4375-8789-e0f80de29cea.pdf",
"size": 48541,
"type": "application/pdf",
"filename": "file.pdf",
"status": "bufferingOrWriting",
"field": "uploadFile"
}
]
}
不知道问题出在哪里,我是不是传文件到服务器了?还是服务器在收到文件后没有正确处理文件?
供参考,这是我的控制器代码:
module.exports = {
upload: function (req, res) {
if(req.method === 'GET')
return res.json({'status':'GET not allowed'});
var uploadFile = req.file('uploadFile');
uploadFile.upload({ dirname: 'assets/images'},function onUploadComplete (err, files) {
if (err) return res.serverError(err);
res.json({status:200,file:files});
});
},
};
我也这样做way.for我的情况很有效;
upload: function (req, res) {
if(req.method === 'GET')
return res.negotiate(new Error('GET not allowed'));
var fileName;
try{
fileName=req.file('uploadFile')._files[0].stream.headers['content-disposition'].split('"').reverse()[1];
}
catch(e){
fileName="abc.abc";
}
req.file('uploadFile').upload({
dirname:'../../tmp',
saveAs:fileName
},function onUploadComplete(err,files){
if(err){
console.log(err);
return res.serverError(err);
}
if(!files.length) {
return res.serverError(new Error('No file Uploaded'))
}
res.ok(files);
});
}
你可以看到这个....
为了关闭它以便它可以帮助其他人,Postman 导致了问题 - 一旦我升级到最新版本,一切都按预期工作。
我正在使用我找到的教程 here to help me figure out file uploads in SailsJS. I have been able to get the uploads working when using the .ejs template engine in Sails, but I need the file uploads to work with the RESTapi. The URL I have setup is 'http://localhost:1337/file/upload',我正在使用 Postman chrome 应用程序将文件发送到服务器,但我得到的响应是:
{
"status": 200,
"file": []
}
当不使用 API(在 .ejs 模板中执行)时,我得到以下响应:
{
"status": 200,
"file": [
{
"fd": "path/to/uploaded/file/.tmp/uploads/assets/images/18c60ef7-b176-4375-8789-e0f80de29cea.pdf",
"size": 48541,
"type": "application/pdf",
"filename": "file.pdf",
"status": "bufferingOrWriting",
"field": "uploadFile"
}
]
}
不知道问题出在哪里,我是不是传文件到服务器了?还是服务器在收到文件后没有正确处理文件?
供参考,这是我的控制器代码:
module.exports = {
upload: function (req, res) {
if(req.method === 'GET')
return res.json({'status':'GET not allowed'});
var uploadFile = req.file('uploadFile');
uploadFile.upload({ dirname: 'assets/images'},function onUploadComplete (err, files) {
if (err) return res.serverError(err);
res.json({status:200,file:files});
});
},
};
我也这样做way.for我的情况很有效;
upload: function (req, res) {
if(req.method === 'GET')
return res.negotiate(new Error('GET not allowed'));
var fileName;
try{
fileName=req.file('uploadFile')._files[0].stream.headers['content-disposition'].split('"').reverse()[1];
}
catch(e){
fileName="abc.abc";
}
req.file('uploadFile').upload({
dirname:'../../tmp',
saveAs:fileName
},function onUploadComplete(err,files){
if(err){
console.log(err);
return res.serverError(err);
}
if(!files.length) {
return res.serverError(new Error('No file Uploaded'))
}
res.ok(files);
});
}
你可以看到这个....
为了关闭它以便它可以帮助其他人,Postman 导致了问题 - 一旦我升级到最新版本,一切都按预期工作。