1
我有一個Meteor應用程序,我試圖從.epub
文件中提取一些圖像,然後將它們保存到Cloudinary,但我沒有任何運氣。以下是我迄今所做:將blob數據上傳到Cloudinary
使用epub.js
我加載.epub
文件並讀取圖像數據:
const book = ePub('path_to_file.epub');
const cover = book.coverUrl();
/* cover._result returns this:
"blob:http://localhost:3000/6433bd4e-13dc-462b-b7be-e9654ad06c18"
*/
我可以手動分配cover._url
到圖像源和圖像呈現爲預期:
$('#bookImage').attr('src', cover._result)
我試圖上傳cover._result
,"blob:http://localhost:3000/6433bd4e-13dc-462b-b7be-e9654ad06c18"
像這樣:
Cloudinary.upload(cover._result,{
folder: 'Books',
resource_type: 'raw',
},function(err, res){
if (err){
console.log(err);
} else {
console.log(res);
}
});
,我沒有得到響應
我也試過這樣:
const myFile = new File([cover._result], "new_file.jpg", {type: "image/jpg"});
Cloudinary.upload(myFile,{...})
的文件上傳,但它的垃圾數據,而不是可讀
最後我試圖轉換文件爲Base64像這樣:
const toDataUrl = function(cover, callback) {
const xhr = new XMLHttpRequest();
xhr.responseType = 'blob';
xhr.onload = function() {
var reader = new FileReader();
reader.onloadend = function() {
callback(reader.result);
}
reader.readAsDataURL(xhr.response);
};
xhr.open('GET', cover);
xhr.send();
}
/******************/
toDataUrl (cover._result, function(base64Img) {
Cloudinary.upload(base64Img,{
folder: 'Books',
resource_type: 'raw',
},function(err, res){
if (err){
console.log(err);
} else {
console.log(res);
}
});
})
我沒有得到Cloudinary.upload
函數的響應。
我的問題是,Cloudinary是否支持這種類型的功能?
謝謝。
解決
這是與流星包本身的問題。
我不得不改變這一點:
toDataUrl (cover._result, function(base64Img) {
Cloudinary.upload(base64Img,{
folder: 'Books',
resource_type: 'raw',
},function(err, res){
if (err){
console.log(err);
} else {
console.log(res);
}
});
})
這樣:
toDataUrl (cover._result, function(base64Img) {
Cloudinary._upload_file(base64Img,{
folder: 'Books',
},function(err, res){
if (err){
console.log(err);
} else {
console.log(res);
}
});
})
並沒有問題,上傳的圖像數據。