I have this short example of using the fileUtils.copyFile function, to copy a text file and an image or binary file, the image file gets converted to a text file, and I really need this function to work for binary files as well, so is there a way to get this to work?
import Felgo 3.0
import QtQuick 2.0
import "Common.js" as Js
App {
// You get free licenseKeys from https://felgo.com/licenseKey
// With a licenseKey you can:
// * Publish your games & apps for the app stores
// * Remove the Felgo Splash Screen or set a custom one (available with the Pro Licenses)
// * Add plugins to monetize, analyze & improve your apps (available with the Pro Licenses)
//licenseKey: "<generate one from https://felgo.com/licenseKey>"
property string assetsBinaryFilePath: ""
property string assetsTextFilePath: ""
Component.onCompleted: {
assetsBinaryFilePath = Qt.resolvedUrl("../assets/felgo-logo.png");
assetsTextFilePath = Qt.resolvedUrl("../assets/test.txt");
console.debug("assetsBinaryFilePath=" + assetsBinaryFilePath)
}
NavigationStack {
Page {
title: qsTr("Test File Utilities")
Grid {
id: grid
anchors.bottomMargin: 6
anchors.rightMargin: 6
anchors.leftMargin: 6
anchors.topMargin: 6
spacing: 6
rows: 3
columns: 2
anchors.fill: parent
AppButton {
id: appButtonWrite
width: 66
text: "Write"
implicitWidth: 66
clip: true
onClicked: {
Js.writeFile("Test/test.txt", "Test File Write Passed");
if (Js.existsFile("Test/test.txt")) {
appTextWrite.text = "Write test Passed";
} else {
appTextWrite.text = "Write test Failed";
}
}
}
AppText {
id: appTextWrite
text: qsTr("Write")
topPadding: 9
}
AppButton {
id: appButtonRead
width: 66
text: "Read"
implicitWidth: 66
clip: true
onClicked: {
appTextRead.text = Js.readFile("Test/test.txt");
}
}
AppText {
id: appTextRead
text: qsTr("Read")
topPadding: 9
}
AppButton {
id: appButtonCopy
width: 66
text: "Copy"
implicitWidth: 66
clip: true
onClicked: {
Js.copyFile(assetsBinaryFilePath, "Test/felgo-logo.png");
if (Js.existsFile("Test/felgo-logo.png")) {
appTextCopy.text = "copyFile: Binary test Passed";
} else {
appTextCopy.text = "copyFile: Binary test Failed";
}
Js.copyFile(assetsTextFilePath, "Test/test.txt");
if (Js.existsFile("Test/test.txt")) {
appTextCopy.text = appTextCopy.text + " Text test Passed";
} else {
appTextCopy.text = appTextCopy.text + " Text test Failed";
}
}
}
AppText {
id: appTextCopy
text: qsTr("Copy")
topPadding: 9
}
}
} // end Page
} // end NavigationStack
} // end App
/*##^## Designer {
D{i:0;autoSize:true;height:480;width:640}D{i:18;anchors_height:400;anchors_width:400}
}
##^##*/
and the Common.js
/* ****************************************************************************
* DebugMessageType: 0=None, 1=Basic, 2=Detailed
*/
var myDebugMessageType = 1;
function getDebugMessageType() {
return myDebugMessageType;
}
function setDebugMessageType(debugMessageType) {
myDebugMessageType = debugMessageType;
}
/* ****************************************************************************
* https://felgo.com/doc/felgo-fileutils/
*/
function writeFile(myFileName, fileContentAsString) {
if (getDebugMessageType() === 1) {
console.debug("writeFile(" + myFileName + "," + fileContentAsString + ")");
}
let fileName = fileUtils.storageLocation(FileUtils.AppLocalDataLocation, myFileName);
return fileUtils.writeFile(fileName, fileContentAsString);
}
/* ****************************************************************************
* https://felgo.com/doc/felgo-fileutils/
*/
function readFile(myFileName) {
if (getDebugMessageType() === 1) {
console.debug("readFile(" + myFileName + ")");
}
let fileName = fileUtils.storageLocation(FileUtils.AppLocalDataLocation, myFileName);
return fileUtils.readFile(fileName);
}
/* ****************************************************************************
* https://felgo.com/doc/felgo-fileutils/
* ../assets/felgo-logo.png
*/
function copyFile(mySource, myDestination) {
if (getDebugMessageType() === 1) {
console.debug("copyFile(" + mySource + ", " + myDestination + ")");
}
let destinationFileName = fileUtils.storageLocation(FileUtils.AppLocalDataLocation, myDestination);
if (getDebugMessageType() === 1) {
console.debug("copyFile(" + mySource + ", " + destinationFileName + ")");
}
return fileUtils.copyFile(mySource, destinationFileName);
}
/* ****************************************************************************
* https://felgo.com/doc/felgo-fileutils/
*/
function existsFile(myFileName) {
if (getDebugMessageType() === 1) {
console.debug("existsFile(" + myFileName + ")");
}
let fileName = fileUtils.storageLocation(FileUtils.AppLocalDataLocation, myFileName);
return fileUtils.existsFile(fileName);
}