Javascript 向函数传递数组而不是字符串以上载文件的解决方案(node.js)
更新了原始问题以尝试简化上下文: 我想将数组中的值作为数组中每个值的函数的第一个参数应用 例如,对于foo中的Javascript 向函数传递数组而不是字符串以上载文件的解决方案(node.js),javascript,arrays,node.js,file-upload,cloudinary,Javascript,Arrays,Node.js,File Upload,Cloudinary,更新了原始问题以尝试简化上下文: 我想将数组中的值作为数组中每个值的函数的第一个参数应用 例如,对于foo中的arg1,So: foo(arg1, arg2, arg3) var myArray = [ 'image1.jpg', 'image2.jpg', 'image3.jpg' ] 将每个图像作为第一个参数应用: foo(image1.jpg, arg2, arg3) foo(image2.jpg, arg2, arg3) foo(image3.jpg, arg2, arg
arg1
,So:
foo(arg1, arg2, arg3)
var myArray = [ 'image1.jpg', 'image2.jpg', 'image3.jpg' ]
将每个图像作为第一个参数应用:
foo(image1.jpg, arg2, arg3)
foo(image2.jpg, arg2, arg3)
foo(image3.jpg, arg2, arg3)
我在想.apply方法可能是实现这一点的一种方法,但无法实现这一点。是否需要一个for循环
例如:
myFunction.apply(this, myArray)
myFunction = foo(this, arg2, arg3)
在这方面:
var oneImageArray = ['lake.jpg', 'pizza.jpg'];
myFunction.apply(this, oneImageArray);
cloudinary.uploader.upload(
this, {folder: "test/name", use_filename: true, unique_filename: false , tags: 'basic_sample'},function(err,image){
console.log();
if (err){ console.warn(err);}
console.log("* "+image.public_id);
console.log("* "+image.url);
waitForAllUploads("test",err,image);
});
原始问题:
我想将一组文件传递给cloudinary的上传功能,这样我就可以上传一批图像(而不是一张可以正常工作的图像)
Cloudinary上载方法执行经过身份验证的上载API调用
通过HTTPS发送图像文件时:
从现在的情况来看,这表明我可以
“将现有图像迁移到Cloudinary,编写一个简短的脚本
遍历您的图像并使用Cloudinary的
上载API。“
因此,我试图通过使用fs readdir recursive
获取指定文件夹中所有文件的数组来实现这一点
当将单个图像指定为upload函数的第一个参数时,该过程将起作用。
我想我可能可以创建一个变量来传递fs readdir返回的数组,但是出现了以下错误
所以我想我的问题是,为什么下面的方法不起作用,是否有一个可行的替代方案来自动化一批文件的处理过程
var dotenv = require('dotenv');
dotenv.load();
var fs = require('fs');
var cloudinary = require('cloudinary').v2;
var uploads = {};
var read = require('fs-readdir-recursive');
var allMyImages = read("src/images");
cloudinary.config({
cloud_name: 'myName',
api_key: 'myKey',
api_secret: 'mySecret'
});
cloudinary.uploader.upload(
allMyImages, {folder: "test/name", use_filename: true, unique_filename: false , tags: 'basic_sample'},function(err,image){
console.log();
if (err){ console.warn(err);}
console.log("* "+image.public_id);
console.log("* "+image.url);
waitForAllUploads("test",err,image);
});
function waitForAllUploads(id,err,image){
uploads[id] = image;
var ids = Object.keys(uploads);
if (ids.length==6){
console.log();
console.log ('** uploaded all files ('+ids.join(',')+') to cloudinary');
performTransformations();
}
}
正如文档中所说,您可以逐个上传图像。 图像阵列上的简单for循环应该可以工作
for(var i = 0; i < allMyImages.length;i++){
cloudinary.v2.uploader.upload(allMyImages[i], options, callback);
}
for(var i=0;i
祝你好运 那很好用-谢谢你。在使用for循环时,我意识到我还没有添加用于作为第一个参数前缀的图像路径,即('src/images/'+imagesInFolder[I],options,callback)。再次感谢。
for(var i = 0; i < allMyImages.length;i++){
cloudinary.v2.uploader.upload(allMyImages[i], options, callback);
}