2 joy460154849 joy460154849 于 2017.09.15 17:19 提问

egg.js报错ctx.oss.put is not a function

图片上传报错;
报错内容:
2017-09-15 17:13:44,493 INFO 1474 [master] egg started on http://127.0.0.1:7001 (1976ms)
field: multiplefile
filename: 111.png
encoding: 7bit
mime: image/png
2017-09-15 17:13:53,619 ERROR 1476 [-/::1/-/27ms POST /upload?_csrf=aUvmihGq--KleIqqIGZRLYsXAM989DojoFUw] nodejs.TypeError: ctx.oss.put is not a function
at HomeController.upload (/Users/lixiuming/egg-example/app/controller/home.js:52:48)
at next (native)
at onFulfilled (/Users/lixiuming/egg-example/node_modules/co/index.js:65:19)
at process._tickCallback (internal/process/next_tick.js:109:7)

pid: 1476

controller:

  • upload() { const ctx = this.ctx; const parts = ctx.multipart(); let part; while ((part = yield parts) != null) { if (part.length) { // 如果是数组的话是 filed console.log('field: ' + part[0]); console.log('value: ' + part[1]); console.log('valueTruncated: ' + part[2]); console.log('fieldnameTruncated: ' + part[3]); } else { if (!part.filename) { // 这时是用户没有选择文件就点击了上传(part 是 file stream,但是 part.filename 为空) // 需要做出处理,例如给出错误提示消息 return; } // part 是上传的文件流 console.log('field: ' + part.fieldname); console.log('filename: ' + part.filename); console.log('encoding: ' + part.encoding); console.log('mime: ' + part.mime); // 文件处理,上传到云存储等等 let result; try { result = yield ctx.oss.put('egg-multipart-test/' + part.filename, part); } catch (err) { // 必须将上传的文件流消费掉,要不然浏览器响应会卡死 // yield sendToWormhole(stream); throw err; } console.log(result); } } console.log('and we are done parsing the form!'); }

1个回答

hhwwyy334
hhwwyy334   2017.10.18 17:36
Csdn user default icon
上传中...
上传图片
插入图片