[英]Get image byte/base64 from URL
I'm looking for a solution where I can provide URL to specific image and then with Javascript I retrieve data of that image. 我正在寻找一种解决方案,可以在其中提供特定图像的URL,然后使用Javascript检索该图像的数据。 I alread found that I can't just pull file through, so maybe byte array or base64 will do?
我已经发现我不能只是拉文件,所以字节数组或base64可以吗?
To be specific before someone downvote that question: I want to parse URL and get image to my server in any form. 具体来说,在有人否决该问题之前:我想解析URL并将图像以任何形式发送到我的服务器。 There is a lot similar questions, but none of them answers this one.
有很多类似的问题,但是没有一个答案。
What I need that for? 我需要什么? I have access to API where I also am provided with image url's, but I want them to be uploaded to my server via
Background Job
in Parse.com service (something like CRON job). 我可以访问还提供图像URL的API,但是我希望它们可以通过Parse.com服务中的
Background Job
(类似于CRON job)上传到我的服务器。 I know how to link file with ParseObject
, but can't find solution how to download image directly to ParseCloud
and link it. 我知道如何使用
ParseObject
链接文件,但是找不到解决方案如何将图像直接下载到ParseCloud
并进行链接。
var Image = require("parse-image");
Parse.Cloud.job("getImagesJob", function(request, status) {
Parse.Cloud.useMasterKey();
var beerObj = Parse.Object.extend("My_class");
var query = new Parse.Query(beerObj);
query.first().then(function(objs) {
var brew = objs.get("brewery");
var bname = objs.get("beer_name");
//var fullName = brew + " " + bname;
var fullName = "Browar Name";
console.log(fullName);
return Parse.Cloud.httpRequest({
method: 'GET',
url: 'api server address',
headers: {
'Content-Type': 'application/json;charset=utf-8'
},
params: {
q : fullName,
client_id : '...',
client_secret : '...'
}
}).then(function(httpResponse) {
var json = JSON.parse(httpResponse.text);
if(json.meta.code === 200){
if(json.response.beers.count > 0){
if(json.response.beers.items[0].beer.beer_label === "/default.png"){
console.log("brak etykiety dla " + fullName);
} else {
console.log("znaleziono etykietę dla " + fullName);
Parse.Cloud.httpRequest({ //NOT REACHING
url: json.response.beers.items[0].beer.beer_label,
success: function(response) {
// The file contents are in response.buffer.
var image = new Image();
return image.setData(response.buffer, {
success: function() {
objs.set("logo", image.data());
console.log("udalo sie dolaczyc");
},
error: function(error) {
// The image data was invalid.
console.error(error);
}
})
},
error: function(error) {
// The networking request failed.
}
});
}
} else {
// daj cokolwiek żeby się nie zacięło na jednym
console.log("Brak danych dla piwa: " + fullName);
}
}
}, function(httpResponse) {
console.error('Request failed with response code ' + httpResponse.status + httpResponse.text);
});
}).then(function(obj) {
status.success("Zrobione");
}, function(error) {
status.error(error);
});
});
You can use parse-image
module in Cloud Code as in their documentation 您可以按照其文档中的说明在Cloud Code中使用
parse-image
模块
var Image = require("parse-image");
Parse.Cloud.httpRequest({
url: YOUR_URL,
success: function(response) {
// The file contents are in response.buffer.
var image = new Image();
return image.setData(response.buffer, {
success: function() {
console.log("Image is " + image.width() + "x" + image.height() + ".");
},
error: function(error) {
// The image data was invalid.
}
})
},
error: function(error) {
// The networking request failed.
}
});
With above code, you can get image data in Buffer using image.data()
. 通过以上代码,您可以使用
image.data()
在Buffer中获取图像数据。 To get base64 data, use image.data().toString("base64")
要获取base64数据,请使用
image.data().toString("base64")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.