如何执行curl -G -o /file/11.png FILE_URL命令
发布于 8 年前 作者 feng12345566 3856 次浏览 来自 问答

本人想从微信多媒体服务器上下载一图片到服务器本地,需使用curl -G -o 11.jpg "http://file.api.weixin.qq.com/cgi-bin/media/get?access_token=ACCESS_TOKEN&media_id=MEDIA_ID"命令来下载 代码如下:

function excuteDownload(file_url, done) {
	var fileName = Date.now() + ".jpg";
	var DOWNLOAD_DIR = "/home/file/";
	var save = '-o ' + DOWNLOAD_DIR + fileName;
	// execute curl using child_process' spawn function  
	var curl = spawn('curl', ['-G', save], [file_url]);
	// add a 'data' event listener for the spawn instance  
	curl.stdout.on('data', function(data) {
		console.log("data", data);
	});
	// add an 'end' event listener to close the writeable stream  
	curl.stdout.on('end', function(data) {
		console.log(fileName + ' downloaded to ' + DOWNLOAD_DIR);
	});
	// when the spawn child process exits, check if there were any errors and close the writeable stream  
	curl.on('exit', function(code) {
		if (code != 0) {
			console.log('Failed: ' + code);
		} else {
			console.log('code: ' + code);
		}
	});?

}

执行返回Failed:2,猜测错误应该在spawn(‘curl’, [’-G’, save], [file_url]);这一句,如何写spawn的参数才能执行curl -G -o 11.jpg "http://file.api.weixin.qq.com/cgi-bin/media/get?access_token=ACCESS_TOKEN&media_id=MEDIA_ID"这一句?

1 回复

var save = DOWNLOAD_DIR + fileName; var curl = spawn(‘curl’, [’-G’, ‘-o’ ,save,file_url]);

改成这样子试试

回到顶部