I couldnt get the "encoding" method that was described here to work, but heres what I was able to do that worked fine for mysql. I read the file into QByteArray and then used bindvalue on the insert/select queries for the array so it wouldnt be ruined:
//addfile.filepath contains the full path+filename IE: c:\folder\test.jpg
bool ProgramMain::addFile() {
//convert file into QByteArray:
QFile file(addfile.
filepath);
//use the file that we selected;
if (file.exists()) {
if (file.
open(QFile::ReadOnly)) {
filedata = file.readAll();
filename = fileinfo.fileName(); //read the name of the file that we are looking for
base = fileinfo.baseName(); //filename
ext = fileinfo.completeSuffix();//extension
file.close();
}
} else {
//alert the user that we selected a file that does not exist for upload
return false;
}
/////////////////////////////////////////////
// here you use QSqlQuery and bindvalue() with your filedata and other info that you collected
///////////////////////////////////////////////
return true;
}
//addfile.filepath contains the full path+filename IE: c:\folder\test.jpg
bool ProgramMain::addFile() {
//convert file into QByteArray:
QByteArray filedata = "";
QString filename = "";
QString base = "";
QString ext = "";
QFile file(addfile.filepath); //use the file that we selected;
QFileInfo fileinfo(addfile.filepath);
if (file.exists()) {
if (file.open(QFile::ReadOnly)) {
filedata = file.readAll();
filename = fileinfo.fileName(); //read the name of the file that we are looking for
base = fileinfo.baseName(); //filename
ext = fileinfo.completeSuffix();//extension
file.close();
}
} else {
//alert the user that we selected a file that does not exist for upload
return false;
}
/////////////////////////////////////////////
// here you use QSqlQuery and bindvalue() with your filedata and other info that you collected
///////////////////////////////////////////////
return true;
}
To copy to clipboard, switch view to plain text mode
then to pull out the data and do something with it:
bool ProgramMain::getFile() {
/////////////////////////////////////////////
// here you use QSqlQuery and query.value(int).toString() for filename,
// and query.value(int).toByteArray() for filedata (the binary data) to pull
// the file out then write it to disk (maybe as a temp file for opening on client
// machine)
///////////////////////////////////////////////
if (file.
open(QFile::WriteOnly)) { file.write(filedata);
file.close();
}
}
bool ProgramMain::getFile() {
/////////////////////////////////////////////
// here you use QSqlQuery and query.value(int).toString() for filename,
// and query.value(int).toByteArray() for filedata (the binary data) to pull
// the file out then write it to disk (maybe as a temp file for opening on client
// machine)
///////////////////////////////////////////////
QFile file(filename);
if (file.open(QFile::WriteOnly)) {
file.write(filedata);
file.close();
}
}
To copy to clipboard, switch view to plain text mode
Bookmarks