필자는 postin을 파일로 업로드하면서 api로 파일을 업로드하는 동안 시트를 엑셀 시트로 가져 와서 json 형식으로 데이터를 반환하는 편안한 API에서 작업하고 있습니다. . API가 추락합니다. 내 창에이 오류가 명령 프롬프트우체부를 사용하는 동안 multer가 req.files를 채우지 않습니다.
app.post('/uploadxl', function(req, res) {
console.log("upload file");
var exceltojson = require("xls-to-json-lc");
upload(req,res,function(err){
console.log("e");
if(err){
res.json({error_code:1,err_desc:err});
return;
}
/** Multer gives us file info in req.file object */
if(!req.file){
res.json({error_code:1,err_desc:"No file passed"});
return;
}
//start convert process
/** Check the extension of the incoming file and
* use the appropriate module
*/
if(req.file.originalname.split('.')[req.file.originalname.split('.').length-1] === 'xlsx'){
exceltojson = xlsxtojson;
} else {
exceltojson = xlstojson;
}
var node_xj = require("xls-to-json");
node_xj({
input: req.file.path, // input xls
output: "output.json", // output json
sheet: "a" // specific sheetname
}, function(err, result) {
if(err) {
console.log("error");
console.error(err);
} else {
console.log("--------result----");
res.json(result);
}
});
});
});
}
:
업로드를 app.js
var app= express();
var upload = multer({
fileFilter : function(req, file, callback) {
if (['xls', 'xlsx'].indexOf(file.originalname.split('.')[file.originalname.split('.').length-1]) === -1) {
return callback(new Error('Wrong extension type'));
}
callback(null, true);
}
}).single('file');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
var routes = require("./routes/routes.js")(app,upload);
var server = app.listen(3000, function() {
console.log("Listening on port %s...", server.address().port);
});
routes.js에서
: 아래
내 코드입니다 파일 e 입력 파일이 누락되었습니다분명히 멀 터의 오류 기능을 입력하고 있음을 의미합니다.
x-www-form-encoded 형식을 사용해도 될까요? –
나는 파일을 업로드 할 수있는 옵션을 얻지 못했지만, 이미 같은 것을 시도했지만 –