CNode

按照koa官方给的例子做文件上传失败,用koa做过文件上传的同学能传授下经验吗?

JJerroldLee发布于12 年前最后回复12 年前12 回复8994 浏览0 收藏

按照官方给的这个例子去做https://github.com/koajs/examples/blob/master/upload/index.js 可是总是获取不到上传的内容,进入不到while里面:

app.use(function *(next){
  // ignore non-POSTs
  if ('POST' != this.method) return yield next;

  // multipart upload
  var parts = parse(this);
  var part;

  while (part = yield parts) {
    var stream = fs.createWriteStream('/tmp/' + Math.random());
    part.pipe(stream);
    console.log('uploading %s -> %s', part.filename, stream.path);
  }

  this.redirect('/');
});
查看回复

回复 (11)

L
littlehaker#1·12 年前

没有进到while里去就是 part = yield parts 没有值,你看看你上传的表单是否有问题

J
JerroldLee#2·12 年前
引用 littlehaker没有进到while里去就是 part = yield parts 没有值,你看看你上传的表单是否有问题

@littlehaker form表单也是仿照例子里写的,可是不知为什么就是不行

J
JerroldLee#3·12 年前
引用 littlehaker没有进到while里去就是 part = yield parts 没有值,你看看你上传的表单是否有问题

@littlehaker 能留个qq或邮箱不?想向你请教详细一点的问题

F
flex1988#4·12 年前

var parts = parse(this); 这句我觉得应该是yield parse(this)

G
gxmari007#5·12 年前

确定是 this.method 而不是 this.request.method 么?

J
JerroldLee#6·12 年前
J
JerroldLee#7·12 年前
Z
zestxjest#8·12 年前

我试了没有问题, 创建了tmp目录没?

J
JerroldLee#9·12 年前
引用 zestxjest我试了没有问题, 创建了tmp目录没?

@zestxjest 创建了,其实单个使用我试过也是没问题的,但整合进项目的时候有问题,找不出原因,所以希望有人可以指点一下

Z
zestxjest#10·12 年前
引用 zestxjest我试了没有问题, 创建了tmp目录没?

@JerroldLee DEBUG一下, 看看this的区别.

参与回复
登录后即可参与回复。登录