Reputation: 117
I created a web app using django. However, I needed a desktop app of the same so I used webkit of PyQt. The whole thing works fine except for file downloads. In my web app the server serves downloadable files at several places. However, when clicking the button that is supposed to trigger the download in the desktop version of the application, nothing happens.
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import *
app = QApplication(sys.argv)
web = QWebView()
web.load(QUrl("http://localhost:8000/home"))
#web.page().setForwardUnsupportedContent(True)
#web.page().unsupportedContent.connect(save_file_callback)
web.show()
sys.exit(app.exec_())
Upvotes: 3
Views: 1569
Reputation: 592
app = QApplication(sys.argv)
web = QWebView(loadFinished=load_finished)
web.load(QUrl("http://localhost:8000/home"))
web.page().setForwardUnsupportedContent(True)
web.page().unsupportedContent.connect(save_file_callback)
web.show()
sys.exit(app.exec_())
def save_file_callback(reply):
try:
with urllib.request.urlopen(reply.url().toString()) as response:
with open('downloaded_file.ext', 'wb') as f:
f.write(response.read())
except Exception as e:
QMessageBox.critical(None, 'Saving Failed!', str(e), QMessageBox.Ok)
def load_finished(reply):
if not reply:
QMessageBox.critical(None, 'Error!', 'An error occurred trying to load the page.', QMessageBox.Ok)
I haven't tested the code but this should get you on the right path. The code is borrowed from my recent project which runs Django projects inside webview as desktop applications - https://github.com/awecode/django-runner
Upvotes: 1