2017-11-22 8 views
0

私のアプリケーションからkoa.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"; 
}); 
関連する問題