No More Posting New Topics!

If you have a question or an issue, please start a thread in our Github Discussions Forum.
This forum is closed for new threads/ topics.

Navigation

    Quasar Framework

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search

    [Solved] [V1] Download a file in cordova app

    Help
    7
    14
    3121
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • M
      Marco last edited by Marco

      Hi all,
      my spa application download a file with the code below:

      this.$axios.post('/api/v1', { url: url, retType: 'excel' },
              { responseType: 'blob' }).then(function (response) {
              const url = window.URL.createObjectURL(new Blob([response.data]))
              const link = document.createElement('a')
              link.href = url
              link.setAttribute('download', fileName)
              document.body.appendChild(link)
              link.click()
              $q.loading.hide()
            }).catch(function (/* error */) {
              $q.loading.hide()
            })
      

      It works fine even in chrome on my android phone (pie).
      I built the android cordova app; it works but when I press the button to download the file it silently fails.
      Do you think the problem is some permission missed on the manifest or do I need to use a cordova plugin ?

      Is this the problem?

      window.URL.createObjectURL()
      

      Thanks

      1 Reply Last reply Reply Quote 0
      • M
        Marco last edited by Marco

        Solved using cordova-plugin-file.
        Replace:

        const url = window.URL.createObjectURL(new Blob([response.data]))
                const link = document.createElement('a')
                link.href = url
                link.setAttribute('download', fileName)
                document.body.appendChild(link)
                link.click()
        
        

        with:

         var blob = new Blob([response.data])
        
         if (typeof cordova !== 'undefined') {
             saveBlob2File(fileName, blob)
         }
        
        

        cordova file plugin code:

             function saveBlob2File (fileName, blob) {
                var folder = cordova.file.externalRootDirectory + 'Download'
                window.resolveLocalFileSystemURL(folder, function (dirEntry) {
                  console.log('file system open: ' + dirEntry.name)
                  createFile(dirEntry, fileName, blob)
                }, onErrorLoadFs)
              }
        
              function createFile (dirEntry, fileName, blob) {
                // Creates a new file
                dirEntry.getFile(fileName, { create: true, exclusive: false }, function (fileEntry) {
                  writeFile(fileEntry, blob)
                }, onErrorCreateFile)
              }
        
              function writeFile (fileEntry, dataObj) {
                // Create a FileWriter object for our FileEntry
                fileEntry.createWriter(function (fileWriter) {
                  fileWriter.onwriteend = function () {
                    console.log('Successful file write...')
                  }
        
                  fileWriter.onerror = function (error) {
                    console.log('Failed file write: ' + error)
                  }
                  fileWriter.write(dataObj)
                })
              }
        
              function onErrorLoadFs (error) {
                console.log(error)
              }
        
              function onErrorCreateFile (error) {
                console.log(error)
              }
        
        I 1 Reply Last reply Reply Quote 1
        • S
          stuffy last edited by

          @Marco Thank you much!

          1 Reply Last reply Reply Quote 0
          • I
            iamyohanarias @Marco last edited by

            @Marco Wonderful my friend but I have a problem. It doesn’t recognize the “cordova” word. I cant put the condition to download for browser and mobile apps. Can you help me?

            dobbel 1 Reply Last reply Reply Quote 0
            • W
              walfin last edited by

              Did you do “cordova plugin add cordova-plugin-file” in src-cordova?

              dobbel I 2 Replies Last reply Reply Quote 0
              • dobbel
                dobbel @walfin last edited by

                This post is deleted!
                1 Reply Last reply Reply Quote 0
                • dobbel
                  dobbel @iamyohanarias last edited by

                  @iamyohanarias that means Cordova is not installed (as a global npm libary).

                  https://cordova.apache.org/docs/en/3.1.0/guide/cli/index.html

                  install cordova globally and make it available to your CLI:
                  npm install -g cordova

                  1 Reply Last reply Reply Quote 0
                  • I
                    iamyohanarias @walfin last edited by iamyohanarias

                    @walfin Yes on an Android platform
                    f6bf2f19-576a-48a9-bec8-ddca0592cb22-image.png

                    dobbel 1 Reply Last reply Reply Quote 0
                    • dobbel
                      dobbel @iamyohanarias last edited by

                      @iamyohanarias is there still a problem?

                      I 1 Reply Last reply Reply Quote 0
                      • I
                        iamyohanarias @dobbel last edited by

                        @dobbel Yes. it doesn’t recognize cordova object even an android app

                        W 1 Reply Last reply Reply Quote 0
                        • W
                          walfin @iamyohanarias last edited by

                          @iamyohanarias What if you try window.cordova instead of cordova?

                          1 Reply Last reply Reply Quote 0
                          • L
                            lscorion93 last edited by

                            hi, on ios at resolveLocalFileSystemURL it throws an exception. Any ideas?

                            1 Reply Last reply Reply Quote 0
                            • K
                              kero last edited by

                              I’m having an error for creating file.

                              dobbel 1 Reply Last reply Reply Quote 0
                              • dobbel
                                dobbel @kero last edited by

                                @kero

                                Please create a new post and explain your problem.

                                1 Reply Last reply Reply Quote 0
                                • First post
                                  Last post