duancan1900 2017-02-16 10:09
浏览 80
已采纳

无法使用Node.js中的请求模块传输大型文件

In a Node.js project I have to transfer file from computer to server. I can send file if file size is small i.e. 2mb but unable to send file if it is more than this size. Here is my code as follows:

var url1 = 'http://beta.xxxxx.com/Xbox/xxxx/index.php/info/xxxxx';
var csvenriched = APPDATApath+'/xxxx/users/'+userId+'/programs/'+programName+'/'+foldername+'/Data_'+tmpstmp+'.csv';

var req = request.post(url1, function (err, resp, body1) {  
    if (err) {
        console.log('REQUEST RESULTS:'+err+resp.statusCode+body1);
        res.send(err); return false;
    } else {
        res.send(body1); return false;
    }
});

var form = req.form();

form.append('file', fs.createReadStream(csvenriched));

On the PHP side where I am sending data code is as follows:

public function actionSavetestvideo() {
    if (!empty($_FILES)) {
        $path = Yii::$app->basePath.'/testfiles/'.$_FILES['file']['name'];
        if (move_uploaded_file($_FILES['file']['tmp_name'], $path)) {
            return 'uploaded';
        } else {
            return 'error'.$_FILES["file"]["error"];
        }
    } else {
        return $_FILES;
    }
}

I know there are answers on internet in case if I have to upload file on Node.js server But in my case I have to transfer file using request module from Node.js to PHP server.

It is working fine in case if file size is small but not if CSV file size is large.

The one thing which I have noticed that if file size is large then if (!empty($_FILES)){} on php side went failed. So I don't think there is issue on PHP side. Please suggest what should I modify there?

  • 写回答

2条回答 默认 最新

  • duanqing2209 2017-02-23 06:36
    关注

    I agreed from mscdex the problem is on PHP side. After increasing upload_max_filesize from 2MB to 50MB it was not working, till then I have restarted the Apache Server again.

    On Restart the Apache server, it starts working perfectly.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?