如何在将文件上传到云服务之前停止并等待管道完成

时间:2016-06-02 10:26:37

标签: node.js asynchronous pipe thunk

我使用 pipe 方法保存前端发送的文件,当我上传保存在本地云端服务的文件时,我不确定文件是否已完成流,所以如何等待 pipe 完成,然后将该文件上传到云服务,最后返回响应

我也尝试编写将文件上传到云服务的步骤,但不起作用

  

res.on( '完成',函数(){

     

...
  })

        var koa-router = require("koa-router");
        var thunkify =require("thunkify");
        var parse = require("co-busboy");

        var parts = parse(this);  // "this" => request 

        while (part = yield parts){

            var imgPath = path.join(__dirname, "../../runtime/img_tmp", new Date().getTime() + "_" + part.filename.toString());
            stream = fs.createWriteStream(imgPath);
            res= part.pipe(stream);
        }
        //check and console.log the file is exist or not
        // upload to cloud service
        console.log(fs.existsSync(imgPath) || path.existsSync(imgPath));

        if(fs.existsSync(imgPath) || path.existsSync(imgPath)){

            var keyName = 'news/image/banner' + imgPath;

            var fileUpload_thunk = thunkify(awsUpload.fileUpload);
            yield fileUpload_thunk(fs.createReadStream(imgPath), keyName);

            console.log(photoUrl);
            //put url to photoUrl 
            photoUrl = 'http://goappdl.goforandroid.com/' + keyName;
            console.log("data-amazionFinished:"+new Date().getTime());
        }
        this.body={"state":1,"url":photoUrl}

0 个答案:

没有答案