我有一个需要通过局域网发送的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
时知道其名称和扩展名?