我正在尝试使用web api files.upload
上传正在上传到我的hapijs服务器的文件。
我得到一条错误消息{"ok":false,"error":"no_file_data"}
。
如果我使用fs.createReadStream
这是我的代码:
const Promise = require('bluebird');
const Joi = require('joi');
const Path = require('path');
const Boom = require('boom');
const Utils = require('../utils');
const Request = require('request');
exports.routes = Promise.coroutine(function*(server, options) {
server.route({
method: 'POST',
path: options.prefix,
config: {
tags: ['api', 'v1'],
payload: {
maxBytes: 2097152,
output: 'stream',
parse: true
},
plugins: {
'hapi-swagger': {
payloadType: 'form'
}
},
validate: {
payload: Joi.object({
cv: Joi.object({ pipe: Joi.func().required() }).required().unknown().description('CV file').meta({ swaggerType: 'file' })
})
},
response: {
emptyStatusCode: 204,
schema: false
},
handler: Utils.coHandler(function*(request, reply) {
const supportedFiles = ['docx', 'doc', 'pdf', 'pages'];
const ext = Path.extname(request.payload.cv.hapi.filename).substr(1);
if (!supportedFiles.filter((supExt) => supExt === ext).length) {
return reply(Boom.badRequest('Unsupported file type'));
}
Request.post({
url: 'https://slack.com/api/files.upload',
formData: {
token: 'XXXX',
filename: `${request.payload.name} - CV.${ext}`,
file: request.payload.cv,
channels: 'XXXX'
}
}, (err, res, body) => {
console.log(body);
});
reply();
})
}
});
});