【发布时间】:2014-06-03 08:48:19
【问题描述】:
我有一个需要通过 LAN 网络发送的 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保存在接收方的工作站中。
所以,我的问题是:当QFile 转换为QByteArray 然后再次(在另一台计算机中)转换为QFile 时,有什么方法可以知道QFile 的名称和扩展名?
【问题讨论】:
标签: c++ qt qtnetwork qfile qbytearray