[Solved] Read local file from QWebView using Ajax request
-
wrote on 11 Jan 2013, 14:57 last edited by
Hi,
I am developing a Qt/C++ program which encapsulates an existing HTML5/JQuery web app.
I used to make Ajax requests to read files from a server. But now, I would like Qt to read a file from the local disk and send its content to my web app.
I think I need Qt to catch Ajax requests from the web app and return the file content as the Ajax request result.
The problem is I don't know how to do. For now, I've not found anything about that on google.
Thanks!
-
wrote on 11 Jan 2013, 15:33 last edited by
You need to subclass QWebPage and reimplement "acceptNavigationRequest":http://qt-project.org/doc/qt-5.0/qtwebkit/qwebpage.html#acceptNavigationRequest function. You can catch requests and reroute them to needed location like file:// etc...
-
wrote on 11 Jan 2013, 16:19 last edited by
[quote author="AcerExtensa" date="1357918430"]You need to subclass QWebPage and reimplement "acceptNavigationRequest":http://qt-project.org/doc/qt-5.0/qtwebkit/qwebpage.html#acceptNavigationRequest function. You can catch requests and reroute them to needed location like file:// etc...[/quote]
Ok thanks a lot, I am going to have a look at this solution.
-
wrote on 14 Jan 2013, 07:58 last edited by
[quote author="AcerExtensa" date="1357918430"]You need to subclass QWebPage and reimplement "acceptNavigationRequest":http://qt-project.org/doc/qt-5.0/qtwebkit/qwebpage.html#acceptNavigationRequest function. You can catch requests and reroute them to needed location like file:// etc...[/quote]
Actually, I can't get into it. Could you write an example of this solution?
First of all, I reimplemented acceptNavigationRequest in a QWebPage subclass but it doesn't catch Ajax requests...
Here is my reimplementation:
@bool MyQWebPage::acceptNavigationRequest(QWebFrame * frame, const QNetworkRequest & request, NavigationType type)
{
QUrl url = request.url();
QString path = url.path();printf("Path is %s and navigation is %d\n", path.toLocal8Bit().constData(), type); if (type == QWebPage::NavigationTypeOther) { if(path == "...") { ... } else if(...) { ... } } return true;
}@
Thanks
-
wrote on 14 Jan 2013, 08:17 last edited by
Your subclass of QWebPage
@
class ExWebPage : public QWebPage;
bool ExWebPage::acceptNavigationRequest(QWebFrame * frame, const QNetworkRequest & request, NavigationType type)
{
/* do what you need here. */
return true;
}
@Set your page to the webview:
@
QWebView * view = new QWebView(this);
ExWebPage * page = new ExWebPage();
view->setPage(page);
@bq. I think I know how to catch the request but how should I reroute it?
for example: you know name of files you need to catch, then in acceptNavigationRequest check request url and if it contains request to that file simply change request URL to the URL where this file should be loaded... for example from file:// url shema for local file access. You will also need to set QWebSettings::LocalContentCanAccessRemoteUrls to true.
But you should know it better how do you should catch and where you should reroute it....
-
wrote on 14 Jan 2013, 08:21 last edited by
In the worst case you can change QNAM of webview to your own, so you can catch any data connections... search this forum for it, some time ago we have made sniffer in this manner...
-
wrote on 14 Jan 2013, 09:23 last edited by
Well I did subclass QNAM and it works fine. But I don't understand why reimplementing acceptNavigationRequest doesn't work with Ajax requests.
-
wrote on 14 Jan 2013, 09:34 last edited by
I think acceptNavigationRequest is only called on user interaction and javascript/ajax bypass it, so you can catch it in createRequest of QNAM....
1/8