有没有办法将一千个图像转储到某个地方并使用REST API来提取它们?

这是事情: –

我有超过一千个图像保存在我的Mac本地。 我有一个着陆页嘲笑电子商务交易网站。 如果要在一千张图片的img标签中手动inputsrc url,将会很繁琐。 因此,我想我可以以某种方式将这些图像转储到云存储中,或者使用REST API获取方法来在response.data中提取这些图像。 然后将其分配给$ scopevariables,并使用ng-repeat绑定我的着陆页视图中的图像。 这可能吗? 如果不是,还有什么select? SQL数据库?

感谢你的帮助。 PS我完全是网页开发的初学者。

安装node.js 这是一个服务器的Javascript,它应该很容易,因为你已经知道Javascript。

在Mac上,您可以像这样安装节点:

 brew install node 

使用这个node.js代码(信用codepedia.com ,稍微调整了一下):

 //include http, fs and url module var http = require('http'), fs = require('fs'), path = require('path'), url = require('url'); imageDir = './images/'; //create http server listening on port 3333 http.createServer(function (req, res) { //use the url to parse the requested url and get the image name var query = url.parse(req.url,true).query; pic = query.image; if (typeof pic === 'undefined') { getImages(imageDir, function (err, files) { var imageList = JSON.stringify(files); res.writeHead(200, {'Content-type':'application/json'}); res.end(imageList); }); } else { //read the image using fs and send the image content back in the response fs.readFile(imageDir + pic, function (err, content) { if (err) { res.writeHead(400, {'Content-type':'text/html'}) console.log(err); res.end("No such image"); } else { //specify the content type in the response will be an image res.writeHead(200,{'Content-type':'image/jpg'}); res.end(content, "binary"); } }); } }).listen(3333); console.log("Server running at http://localhost:3333/"); //get the list of jpg files in the image dir function getImages(imageDir, callback) { var fileType = '.jpg', files = [], i; fs.readdir(imageDir, function (err, list) { for(i=0; i<list.length; i++) { if(path.extname(list[i]) === fileType) { files.push(list[i]); //store the file name into the array files } } callback(err, files); }); } 

从命令行运行这个命令启动你的新镜像服务器(假设你命名为“server.js”):

 node server.js 

你应该看到这个文本出现在命令行上:

 Server running at http://localhost:3333/ 

您可以通过在浏览器中转到此地址来快速testing它,并且您应该看到一个JSON对象,显示“./images”目录中所有文件名的数组。 顺便说一句,这个程序假定你把图像文件夹放在与“server.js”相同的目录中。 您可以将images目录放在任何位置,只需更改variables“imageDir”的path即可。

现在你可以使用这个代码在你的控制器中加载Angular的文件列表:

 $http.get("http://localhost:3333", function(data) { $scope.images = data; }); 

在你看来,你现在可以像这样使用ng-repeat来显示所有的图像:

 <div ng-repeat="image in images" style="padding: 8px"> <img src="http://localhost:3333/image={{ image }}"> </div> 

注意:如果您在Mac上本地运行它,或者如果您将所有图像上载到可以使用Node.js的服务器,则这将起作用。