UBports Robot Logo UBports Forum
    • 版面
    • 最新
    • 標籤
    • 熱門
    • 使用者
    • 群組
    • 搜尋
    • 註冊
    • 登入

    Handle downloads with a WebView

    已排程 已置頂 已鎖定 已移動 Solved App Development
    4 貼文 2 Posters 491 瀏覽 2 Watching
    正在載入更多貼文
    • 從舊到新
    • 從新到舊
    • 最多點贊
      回覆
      • 在新貼文中回覆
      登入後回覆
      此主題已被刪除。只有擁有主題管理權限的使用者可以查看。
      • Schlicki2808S 離線
        Schlicki2808
        最後由 編輯

        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 條回覆 最後回覆 回覆 引用 0
        • CiberSheepC 離線
          CiberSheep @Schlicki2808
          最後由 編輯

          @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 條回覆 最後回覆 回覆 引用 0
          • Schlicki2808S 離線
            Schlicki2808 @CiberSheep
            最後由 編輯

            @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 條回覆 最後回覆 回覆 引用 0
            • Schlicki2808S 離線
              Schlicki2808
              最後由 編輯

              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 條回覆 最後回覆 回覆 引用 0
              • Schlicki2808S Schlicki2808 marked this topic as a question on
              • Schlicki2808S Schlicki2808 has marked this topic as solved on
              • 第一個貼文
                最後的貼文