我使用 SQLite 原生插件 来存储文本内容,但我也需要对图像进行存储! (对于离线模式)
使用 angular/ionic 4 无法将图像转换为 base64 字符串,所以我的问题是:
如何使用 ionic/Cordova 提供的原生文件传输插件从来自 Firebase 的图片 url 下载图片
我已经阅读了文档,但它是基础的,不要给出太多!
DOCS 提供的代码:( 我不明白 file.pdf
的下载方法!图像可以带有任何扩展名,例如 jpg/png ..等 )
download() {
const url = 'http://www.example.com/file.pdf';
fileTransfer.download(url, this.file.dataDirectory + 'file.pdf').then((entry) => {
console.log('download complete: ' + entry.toURL());
}, (error) => {
// handle error
});
}
最佳答案
file.pdf
下载方法不是问题,因为您可以从 API 响应中获取带有扩展名的完整文档/图像名称。
例如
for (let i = 0; i < res.data.length; i++) {
let fileName = res.data[i].name;
let url = 'http://www.example.com/' + res.data[i].name;
const _fileTransfer: FileTransferObject = this._fileTransfer.create();
_fileTransfer.download(url, this._file.dataDirectory + fileName).then((entry) => {
console.log('download complete: ' + entry.toURL());
}, (error) => {
console.log(error);
});
}
然后您可以使用 normalizeURL 显示图像,给定一个表示 URL 的字符串,returns the URL path after stripping the trailing slashes .
import { normalizeURL } from 'ionic-angular';
{'filePath': normalizeURL(result[i].fileName)
<img src="{{filePath}}">
关于javascript - ionic 4 : How to save images on device for offline mode that comes from firebase storage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55836425/