我有一个QFile
需要通过局域网发送的。为此,我通过以下方式将其转换QFile
为QByteArray
:
//! [Inside a QTcpSocket class]
// Get the file name using a QFileDialog
QFile file(QFileDialog::getOpenFileName(NULL, tr("Upload a file")));
// If the selected file is valid, continue with the upload
if (!file.fileName().isEmpty) {
// Read the file and transform the output to a QByteArray
QByteArray ba = file.readAll();
// Send the QByteArray
write(ba);
}
当我收到它时,我可以使用以下方法轻松转换它:
void saveFile(QByteArray ba) {
// Ask the user where he/she wants to save the file
QFile file(QFileDialog::getSaveFileName(NULL, tr("Save file")));
// Check that the path is valid
if (!file.fileName().isEmpty()) {
// Write contents of ba in file
file.write(ba);
// Close the file
file.close();
}
}
但是,我想知道文件名(例如Document.docx
)或至少知道它的扩展名,以避免强迫用户确切知道他收到的文件类型。
理想情况下,当文件上传时,会提示接收方用户保存文件。例如:
- 发件人发送
Document1.docx
- 接收者会收到提示,如果他/她想保存
Document1.docx
- 根据接收者的决定,
Document1.docx
保存在接收者的工作站中。
所以,我的问题是:有什么方法可以知道 aQFile
转换为 aQByteArray
然后再次(在另一台计算机中)转换为 a 时的名称和扩展名QFile
?