在Linux下使用qt开发的聊天软件,为什么一端发向另一端doc文件可以,另一端向这一端发送就没有数据;但txt文件互传是可以的
if(msgType == "receiveFile"){//文件处理
QString sendUser,receiveUser,fileStr;
QByteArray fileData;
in >> sendUser >> receiveUser >> fileStr >>fileData;
if(!fileData.isEmpty()){
QString fileName = QFileDialog::getExistingDirectory();//选择文件目录
qDebug()<<fileName;
QFile file(fileName+"/"+fileStr);
file.open(QIODevice::WriteOnly);
file.write(fileData);//保存文件
file.close();
}
}
fileData.isEmpty() 在doc文件时是ture,在txt时是false
一直查找不出来错
可以互传doc文件
txt文本是可见字符的数据流,doc是二进制数据流.
在发送的时候时候是使用了可见字符数据流发送方式,导致无法接受数据.
需求
为什么一端发向另一端doc文件可以,另一端向这一端发送就没有数据;但txt文件互传是可以的
答案
1、doc和txt文件格式不同,一般txt可以直接字符文本传输,而doc或者pdf、img这种文件都需要转换为二进制数据流
2、既然A -> B doc可以发送,B->A发送不了
前者说明A->B对于二进制文件处理是正确的,
后者修改对应文件处理代码跟前者相似就可以了。
QAxObject wordApplication("Word.Application");
QAxObject *documents = wordApplication.querySubObject("Documents");
QAxObject *document = documents->querySubObject("Open(const QString&, bool)", "C:\ForResume\2.docx", true);
QAxObject *words = document->querySubObject("Words");
QString textResult;
int countWord = words->dynamicCall("Count()").toInt();
for (int a = 1; a <= countWord; a++){
textResult.append(words->querySubObject("Item(int)", a)->dynamicCall("Text()").toString());
}
qDebug()<<textResult;
可以尝试以二进制流打开文件,进行数据传输