Phonegap - How to access file in www-folder?

前端 未结 5 858
终归单人心
终归单人心 2021-01-06 12:11

I saw multiple solutions how to access a file in the www folder but no solution works for me. I test the application under iOS with the iOS-simulator.
I want to access

5条回答
  •  温柔的废话
    2021-01-06 12:39

    One trick that works is to fs.download each file from the www folder into Cordova’s persistent file system. See my original post.

    First, in Terminal:

    1. npm install cordova-promise-fs
    2. cordova plugin add cordova-plugin-file --save
    3. cordova plugin add cordova-plugin-file-transfer --save

    Then, in your front-end:

    import CordovaPromiseFS from 'cordova-promise-fs'
    
    const fs = CordovaPromiseFS({
      persistent: true,
      storageSize: 200 * 1024 * 1024,
      concurrency: 3
    })
    

    If you use React, the above has to be declared before the component Class is created, while the below code should be in its own function inside the component Class. See my GitHub comment for more details.

    window.resolveLocalFileSystemURL(
      cordova.file.applicationDirectory + 'www/epubs/alice.epub',
        // If successful...
        (fileSystem) => {
          const downloadUrl = fileSystem.toURL()
          const localUrl = 'alice.epub' // the filename it is stored as in the device
          fs.download(
            downloadUrl,
            localUrl,
            (progressEvent) => {
              if (progressEvent.loaded && progressEvent.total) {
              console.log('progress', Math.round((progressEvent.loaded / progressEvent.total) * 100))
            }
          }
        ).then((filedata) => {
          return fs.toInternalURL(localUrl)
        })
        .then((localPath) => {
          this.setState({ epubPath: localPath })
        }).catch((error) => {
          console.log('some error happend', error)
        })
      },
      // If unsuccessful
      (err) => {
        console.log(err)
      }
    )
    

提交回复
热议问题