How to transfer a file from one server to another using node.js

耗尽温柔 提交于 2019-12-23 10:14:26

问题


I apologize if this question was asked elsewhere, but I couldn't find a suitable solution to this vexing problem so here's my situation.

I have a node.js script that creates an excel document from scratch and everything is working as expected. However, I'm having trouble saving this newly created file into another remote server which is running ColdFusion (not sure if this matters but i figured I'd at least mention it).

The following code produces a request from node.js, but coldfusion keeps telling me that there are no files to parse. Am I missing something fundamental?

Node.js

var excel_hook = "http://localhost/models/post_hooks/excel.cfc?method=getReportDataAjax";

var data = {
    excelFile: {
        // Hard Coding this to see why the post is NOT picking up the file
        file: "temp/check_history_test.xlsx",// + file_name,
        content_type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
    }
};

    var requestOptions = {
    host: "http://localhost",
    path: "/excel.cfc?method=getReportDataAjax",
    method: "POST"
};
var request = http.request(requestOptions, function(res){
    res.setEncoding('utf8');
    res.on('data', function(chunk){
        console.log(chunk);
    });
    res.on('end', function(err, body){
        callback(err, body);
    });
});
request.on("error", function(err){
    console.error("Request Failed:" + err);
});
--

var fileStream = fs.createReadStream("temp/" + file_name);
fileStream.on('data', function(data){
    console.log("File Data:");
    console.log(data);
    request.write(data);
});

fileStream.on('error', function(err){
    console.error("File Error:", err);
    throw err;
});

fileStream.on('end', function(){
    request.end();
});

ColdFusion

component extends="models.models"{

/**
Main remote method called by the javascript controller
*/
remote function getReportDataAjax() returnFormat="JSON"{

    var reportData = uploadExcel(form.excelFile);

    reportData = SerializeJSON(reportData);

    return reportData;
}//end



/**
Returns struct containing all data - called by the remote method above
*/
public function uploadExcel(required excelFile)
{


    var returnObj = fileUpload("temp", excelFile, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
    return {
        "bSuccess": true
    };
}

}//end component

来源:https://stackoverflow.com/questions/25315630/how-to-transfer-a-file-from-one-server-to-another-using-node-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!