【问题标题】:Uploading file to koa-js server with formidable强大的将文件上传到koa-js服务器
【发布时间】:2017-11-22 13:47:39
【问题描述】:

我正在尝试将文件从我的 Ionic 应用程序上传到基于 koa.js 的节点 js 服务器。用于使用 koa-body 和强大的解析 body im。

这是我的服务器:

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))
}

一切正常,但没有创建文件,没有显示错误,什么也没有。

有什么想法吗?

【问题讨论】:

    标签: javascript node.js angular koa formidable


    【解决方案1】:

    一段时间后,我能够解决文件上传问题。设置原点后,我不得不移动强大的中间件。另外,我不得不将 koaBody 函数移到 post 操作中

    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";
    });
    

    【讨论】:

    • 上传前有什么方法可以验证文件吗?我试图指定formidable: { onPart: function(part) {/* validate part.filename here.. /*} } 但是,它不是可达语句。
    猜你喜欢
    • 2011-11-16
    • 1970-01-01
    • 1970-01-01
    • 2019-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-22
    • 2017-04-05
    相关资源
    最近更新 更多