UBports Robot Logo UBports Forum
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Register
    • Login

    Handle downloads with a WebView

    Scheduled Pinned Locked Moved Solved App Development
    4 Posts 2 Posters 870 Views 2 Watching
    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.
      • Schlicki2808S Offline
        Schlicki2808
        last edited by

        Hello,
        currently, I am migrating my apps from HTML template to QML with WebView because I need the XMLHttpRequest. Now I want to use a function where some data will be downloaded to the device. I used this in an HTML template which worked before

        function downloadIcsFile() {
            var state = document.getElementById("state").value;
            var year = document.getElementById("year").value;
            var element1 = document.createElement('a');
            element1.setAttribute('href', 'data:text/calendar;charset=utf-8,' + encodeURIComponent(icsContent));
            element1.setAttribute('download', "ferien-" + state + "-" + year + ".ics");
            element1.style.display = 'none';
            document.body.appendChild(element1);
          
            element1.click();
          
            document.body.removeChild(element1);
        }
        

        In my QML app with WebView it does nothing. I've learned that a WebView can't handle downloads by itself. But I did not find any example for documentation on how to do this. So, how can I implement the download function?

        Thanks in advance!

        CiberSheepC 1 Reply Last reply Reply Quote 0
        • CiberSheepC Offline
          CiberSheep @Schlicki2808
          last edited by

          @schlicki2808 The code might be a bit tangled but I hope it helps: https://gitlab.com/cibersheep/quixe-qml/-/tree/master/qml

          Another planet, another time, another universe!

          Schlicki2808S 1 Reply Last reply Reply Quote 0
          • Schlicki2808S Offline
            Schlicki2808 @CiberSheep
            last edited by

            @cibersheep Thanks, this is a good starting point. I've tried around with SingleDownload and DownloadManager component. As long as the content to download comes from a web url, it works fine, but I have much difficulties with data urls.
            E.g.:
            This works:

            element1.setAttribute('href', 'data:;base64,' + btoa("Hello World! What's up with you?"));
            

            This doesn't:

            element1.setAttribute('href', 'data:;base64,' + btoa(icsContent));
            

            (I left out the mime types because it didn't make any difference)

            The only difference is that icsContent contains a long string with line breaks (\n) but apart from that it will also be converted to base64.

            My last idea is to create a web service, post the base64 to it and fetch the result by SingleDownload. That means that such a download requires internet which is not nice in this case.

            Is there another trick I can use?

            1 Reply Last reply Reply Quote 0
            • Schlicki2808S Offline
              Schlicki2808
              last edited by

              So, I finally found a solution. Okay, in my case, the constellation is a bit different: I have a qml app with a WebView. I ended up to trigger the download from HTML/Javascript side and to process the download on qml side. I failed to use the SingleDownload component because it can not handle data-URLs. So I implemented my own solution:

              // ...
                      WebContext {
                          id: mainContext
                          onDownloadRequested: {
                              var fileUrl = "/home/phablet/.local/share/schulferien.daniel/Downloads/" + download.downloadFileName;
                              var request = new XMLHttpRequest();
                              request.open("PUT", fileUrl, false);
                              request.send(decodeURIComponent(download.url.toString().replace("data:text/plain;,", "")));
                              PopupUtils.open(downloadedMessage);
                          }
                      }
              
                      WebView {
                          id: mainWebView
                          context: mainContext
              // ...
              
              

              The "fileUrl" has to point to the application path + Downloads, otherwise a "device not open" error will occur.

              1 Reply Last reply Reply Quote 0
              • Schlicki2808S Schlicki2808 marked this topic as a question on
              • Schlicki2808S Schlicki2808 has marked this topic as solved on

              Hello! It looks like you're interested in this conversation, but you don't have an account yet.

              Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

              With your input, this post could be even better 💗

              Register Login
              • First post
                Last post