2017-11-22 91 views
0

ko.js를 기반으로 이오닉 응용 프로그램에서 노드 js 서버로 파일 업로드를 시도하고 있습니다. koa-body와 formidable을 사용하여 본문을 구문 분석합니다. 여기 koa-js 서버에 파일 업로드 중

내 서버입니다 :

this.app.use(formidable()); 

this.app.use(koaBody({ 
    formidable:{ 
     uploadDir: __dirname + '/uploads', // directory where files will be uploaded 
     keepExtensions: true, // keep file extension on upload 
     multiples: true 
    }, 
    multipart: true, 
    urlencoded: true, 
    formLimit: '5mb', 
})); 

this.app.use(_.post('/wish/photo/upload', async (ctx, next) => { 
     //ctx.body = JSON.stringify(ctx.request); 
     next(); 
    })); 

이는 프론트 엔드에 내 파일 업로드 기능입니다 :

uploadFromPc(files, parameters){ 
    let headers = new Headers(); 
    let formData = new FormData(); 
    Array.from(files).forEach(file => formData.append('photo', file)); 
    let options = new RequestOptions({ headers: headers }); 
    //options.params = parameters; 
    return this.http.post(EnvVariables.apiEndpoint + 'wish/photo/upload', formData, options) 
      .subscribe(response => console.log(response)) 
} 

모든 것이 예상대로 진행하지만, 파일이 생성되지 않는 경우, 오류가 없습니다 표시, 아무것도.

아이디어가 있으십니까?

답변

0

잠시 후 파일 업로드 문제를 해결할 수있었습니다. 나는 원산지 설정 후 강력한 미들웨어를 이동해야했습니다. 또한 koaBody 기능을 게시 작업으로 이동해야했습니다.

router.post('/wish/photo/upload', koaBody({ 
    formidable: { 
     uploadDir: __dirname + '/uploads', // directory where files will be uploaded 
     keepExtensions: true, // keep file extension on upload 
     multiples: true, 
    }, 
    multipart: true, 
    urlencoded: true, 
    formLimit: '5mb', 
}), (ctx, next) => { 
    ctx.body = "Success"; 
});