2017-12-30 46 views
0

필자는 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 입력 파일이 누락되었습니다

분명히 멀 터의 오류 기능을 입력하고 있음을 의미합니다.

enter image description here

+0

x-www-form-encoded 형식을 사용해도 될까요? –

+0

나는 파일을 업로드 할 수있는 옵션을 얻지 못했지만, 이미 같은 것을 시도했지만 –

답변

0

귀하의 문제는 this 질문과 비슷한 것 같다.

우편 배달부를 사용하는 경우 헤더 : "Content-type": "multipart/form-data"를 제거해보십시오.

enter image description here

+0

제안을 주셔서 감사합니다. 그러나 여전히 동일한 결과를 얻고 있습니다. –

+0

예. 파일을 가져 오기 위해 지시문을 작성하고 싶습니다. https : // stackoverflow가 있습니다. com/a/34205893/7862193 –

+0

코드 (nodejs)에 문제가 있는지를 알아 내려고합니다.이 질문은 angularjs가 아닙니다. –