Results 1 to 8 of 8

Thread: Java script accessing local files in a webview?

  1. #1
    Join Date
    Feb 2008
    Posts
    154
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    12

    Question Java script accessing local files in a webview?

    Hi guys,

    I am trying to run pdf.js (a library built using js and html5 to show pdf inside a webview)
    in a webview but the js is not able to access local pdf files

    I am using Qt quick 2

    How could i change the security permissions from the webview to be able to access local files?

  2. #2
    Join Date
    Feb 2008
    Posts
    154
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    12

    Default Re: Java script accessing local files in a webview?

    Can any body help?

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Java script accessing local files in a webview?

    What does pdf.js use to load local files? As far as I know JavaScript has no API to access local files.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. #4
    Join Date
    Feb 2008
    Posts
    154
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    12

    Default Re: Java script accessing local files in a webview?

    Thank you Wysota for your support.

    Here is how pdf.js loading files
    Qt Code:
    1. var xhr = new XMLHttpRequest();
    2. xhr.onload = function() {
    3. PDFView.open(new Uint8Array(xhr.response), 0);
    4. };
    To copy to clipboard, switch view to plain text mode 

    It loads the file if i used an online pdf but it does not load local files.
    as i know i should enable accessing local files in the webview but this feature is not available in Qt quick 2 webview.

    BR

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Java script accessing local files in a webview?

    I don't think it is available in any default web browser. I'm afraid that if you want something like that, you will have to implement it yourself. But I don't think QtQuick's WebView exposes enough API to do this.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #6
    Join Date
    Feb 2008
    Posts
    154
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    12

    Default Re: Java script accessing local files in a webview?

    Any suggestions how to implement it?

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Java script accessing local files in a webview?

    Quote Originally Posted by mismael85 View Post
    Any suggestions how to implement it?
    You can try substituting network access manager object for your QML engine to serve you the data and/or modify the javascript library to use URLs the default network access manager can understand.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #8
    Join Date
    Feb 2008
    Posts
    154
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    12

    Default Re: Java script accessing local files in a webview?

    I have found a solution you can post messages to webview through the experimental APIs also you can receive messages here is a code sample

    main.qml
    Qt Code:
    1. import QtQuick 2.2
    2. import QtQuick.Window 2.1
    3. import QtWebKit 3.0
    4. import QtWebKit.experimental 1.0
    5.  
    6. Window {
    7. visible: true
    8. width: 360
    9. height: 360
    10.  
    11. WebView{
    12. id: webview
    13. anchors.fill: parent
    14. url: "file:///D:/workspace/pdf.js/web/viewer.html"
    15.  
    16. experimental.preferences.navigatorQtObjectEnabled: true
    17. }
    18.  
    19. MouseArea {
    20. anchors.fill: parent
    21. onClicked: {
    22. var data = pdfLoader.loadPDF( "D:/Secrets_of_Success.pdf" );
    23. webview.experimental.postMessage( data );
    24. console.log( "data= " + data );
    25. }
    26. }
    27. }
    To copy to clipboard, switch view to plain text mode 

    and here is the js file
    Qt Code:
    1. navigator.qt.onmessage = function(ev) {
    2. //eval(ev.data);
    3.  
    4. var data = ev.data;
    5. var buf = new ArrayBuffer(data.length * 2); // 2 bytes for each char
    6. var bufView = new Uint16Array(buf);
    7. for (var i=0, strLen = data.length; i<strLen; i++) {
    8. bufView[i] = data.charCodeAt(i);
    9. }
    10. PDFJS.getDocument(buf);
    11. }
    To copy to clipboard, switch view to plain text mode 

    but still i have a problem sending the file to pdf.js library
    I am reading the file and send the QBytearray directly to the js file (UInt8Array) but it is not able to load the pdf file

    Do have any ideas how to could i read the file and send it to the pdf library?

Similar Threads

  1. Accessing sql local storage from C++?
    By Koying in forum Qt Quick
    Replies: 1
    Last Post: 27th February 2014, 20:49
  2. Qml java script
    By ganeshgladish in forum Newbie
    Replies: 6
    Last Post: 7th June 2013, 16:52
  3. java script function
    By ganeshgladish in forum Newbie
    Replies: 1
    Last Post: 5th June 2013, 13:19
  4. Java Script on Qt4.5
    By kavinsiva in forum Newbie
    Replies: 1
    Last Post: 7th October 2009, 13:08
  5. Local flashfile not playing in webView
    By AnAx in forum Qt Programming
    Replies: 1
    Last Post: 4th October 2009, 03:59

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.