Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Post #173

Closed
wants to merge 3 commits into from
Closed

Post #173

wants to merge 3 commits into from

Conversation

viRingbells
Copy link
Contributor

增加了对多种类型post数据的支持,

现在可以使用https://github.com/koajs/koala/blob/master/docs/body-parsing.md#content-negotiation这个方式来进行处理post数据。

同时将上面的方案集成到了lark中,具体方案如下:

增加* this.request.body()函数,在启用parser的情况下,yield this.request.body()能根据数据的类型做处理,返回对应的结果。

不过由于image/*multipart类型中会有文件,yield this.request.body()会将这些文件转换成Buffer,可能会造成内存占用过高。

所以对于这种情况,既可以通过配置的方式disable掉parse的工作,直接对stream进行操作,也可以在调用yield this.request.body()前设置this.request.saveAs = function (readabel) { ... }来设置保存的路径计算方式,这样lark会自行将上传对文件按照saveAs计算的路径保存到本地

方案效果

router.post('/', function *(next) {
    this.request.saveAs = function (readable) {
        return 'upload/' + readable.filename;
    };
    var postData = yield this.request.body();
    console.log(postData);
    this.body = 'UPLOAD OK!';
    yield next;
});

结果

{
  name: 'another',
  userfile: 
  {
    filename: 'haha.jpg',
    name: 'userfile',
    mime: 'image/jpeg',
    mimeType: 'image/jpeg',
    save: '/Users/mbu-se-vc/haohao/projects/appPost/upload/haha.jpg' 
  },
  type: 'image'
 }

@zezhou 看看方案是否ok

@viRingbells
Copy link
Contributor Author

var postData = yield this.request.body()虽然和之前的var postData = yield this.request.body很像,不过毕竟不能兼容,我再优化一下方案。
先不merge了,明天再弄。

@zezhou
Copy link
Contributor

zezhou commented Sep 22, 2015

可以用新语法 set body(){} 来使 this.request.body == this.request.body() 吧

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants