如果您的文件仅为文本,您可以使用XMLHttpRequest(用于读取和写入),像这样:
如果您的文件仅为文本,您可以使用XMLHttpRequest(用于读取和写入),像这样:
function openFile(fileUrl) {
var request = new XMLHttpRequest();
request.open("GET", fileUrl, false);
request.send(null);
return request.responseText;
}
function saveFile(fileUrl, text) {
var request = new XMLHttpRequest();
request.open("PUT", fileUrl, false);
request.send(text);
return request.status;
}
这是演示应用程序(Qt 5.6):
import QtQuick 2.6
import QtQuick.Dialogs 1.2
import QtQuick.Controls 1.5
ApplicationWindow {
visible: true
width: 640
height: 480
title: qsTr("Demo App")
function openFile(fileUrl) {
var request = new XMLHttpRequest();
request.open("GET", fileUrl, false);
request.send(null);
return request.responseText;
}
function saveFile(fileUrl, text) {
var request = new XMLHttpRequest();
request.open("PUT", fileUrl, false);
request.send(text);
return request.status;
}
FileDialog {
id: openFileDialog
nameFilters: ["Text files (*.txt)", "All files (*)"]
onAccepted: textEdit.text = openFile(openFileDialog.fileUrl)
}
FileDialog {
id: saveFileDialog
selectExisting: false
nameFilters: ["Text files (*.txt)", "All files (*)"]
onAccepted: saveFile(saveFileDialog.fileUrl, textEdit.text)
}
menuBar: MenuBar {
Menu {
title: qsTr("File")
MenuItem {
text: qsTr("&Open")
onTriggered: openFileDialog.open()
}
MenuItem {
text: qsTr("&Save")
onTriggered: saveFileDialog.open()
}
MenuItem {
text: qsTr("Exit")
onTriggered: Qt.quit();
}
}
}
TextArea {
id: textEdit
anchors.fill: parent
text:
"Lorem ipsum dolor sit amet, consectetur adipisicing elit, " +
"sed do eiusmod tempor incididunt ut labore et dolore magna " +
"aliqua. Ut enim ad minim veniam, quis nostrud exercitation " +
"ullamco laboris nisi ut aliquip ex ea commodo cosnsequat. ";
}
}
注意,所有现代浏览器如果尝试使用上述函数将会抛出安全异常,但是QML允许使用(即使进行文件重写)。虽然不确定是否设计如此,还是错误。