我想使用Node和请求模块将一个远程映像上传到我自己的服务器。我已经知道如何使用以下代码上传本地图像:
var options = {
url: 'https://myownserver.com/images'
};
var req = request.post(options , function optionalCallback(err, httpResponse, body) {
console.log('Upload successful! Server responded with:', body);
});
var form = req.form();
form.append('file', fs.createReadStream(__dirname + '/testimg.png'));为了能够上传远程图像,我需要对这些代码进行哪些修改?这是我一直在使用的图像:https://www.filepicker.io/api/file/egqDUkbMQlmz7lqKYTZO
我尝试过在远程URL上使用fs.createReadStream,但没有成功。如果可能的话,在将图像上传到我自己的服务器之前,我不希望在本地保存它。
发布于 2015-10-17 01:49:17
这是我和刮刀一起使用的一段代码。我在这里使用回调,以便在保存到我的模型时可以使用这个位置。我把你的图片的位置放在下面,但是在我的代码中我使用req.body.image。
var downloadURI = function(url, filename, callback) {
request(url)
.pipe(fs.createWriteStream(filename))
.on('close', function() {
callback(filename);
});
};
downloadURI('https://myownserver.com/images', __dirname + '/testimg.png', function(filename) {
console.log(done);
}https://stackoverflow.com/questions/33181758
复制相似问题