如何使用离子4将任何类型的文件存储在pouch db中?

问题描述 投票:0回答:1

这是我尝试的方式

await this.base64.encodeFile(uri).then((base64File: string) => {
      console.log(base64File);
      base64_data = base64File
      alert(JSON.stringify(base64_data));
      console.log(base64_data);
    }, (err) => {
      console.log(err);
      alert(JSON.stringify(err));
    });

this.db.putAttachment(att_id,att_name,base64_data, att_file_type).then(function (result) {
  console.log(result);
  alert(JSON.stringify(result)); 
}).catch(function (err) {
  // ouch, an error
  console.log(err);
  alert(JSON.stringify(err));
});

它适用于图像文件,但对于pdf文件返回“”

base64 ionic4 pouchdb
1个回答
0
投票

您可以将文件数据转换为二进制数据或base64

此示例转换为二进制数据

  • 第一个带有XHR请求的下载文件可以使用jquery ajax,axios或...
  • 设置responseType: 'arraybuffer'
  • 下载后或下载前,请在pouch db中创建文档
    axios.get(url, {
                    progress: false, responseType: 'arraybuffer',
                    onDownloadProgress: (progressEvent) => {
                        precent = (100 * progressEvent.loaded / progressEvent.total)
                        console.log(precent)
                    }
                })
.then(resp => {

                    //get db
                    let db = $db.dbModel

                    //set attach
                    db.get(doc._id).then((doc) => {

                        db.putAttachment(doc._id, 'index.mp4', doc._rev, new Blob([new Uint8Array(resp.data)], {type: 'video/mp4'}), 'video/mp4')
                            .then(res => {
                                // console.log('success store file')
                            })
                    })

                })

https://github.com/mohammadnazari110/pwa_offline_video_download

© www.soinside.com 2019 - 2024. All rights reserved.