数据上传,下载(二)

数据处理流程

1读取文件函数

//上传时压缩数据
QByteArray UpLoadDialog::compressFile( const QString& path )
{
	QFile file(path);  
	QByteArray qCompressData;
	if(file.open(QIODevice::ReadOnly))
	{  
		QByteArray data = file.readAll();  

		qCompressData = qCompress(data, 9);  
	}
	file.close();
	qDebug()<<"qCompressData"<<qCompressData.size();
	return qCompressData;	
}
2下载后,,传入保存路径,与下载的二进制文件,解压后保存文件,提示下载文成.

//下载时解压数据
bool UpLoadDialog::uncompressFile( const QString& path, const QByteArray& data )
{
	if (!path.isEmpty() && !data.isEmpty())
	{
		QByteArray undata = qUncompress(data);
		if (!undata.isEmpty())
		{
			QFile file(path); 
			if(file.open(QIODevice::WriteOnly))  
			{  
				file.write(undata);  
				file.close();  
				QMessageBox::warning(0, "提示", "选择的文件下载完成!");
				return true;
			} 
			return false;
		}
		return false;
	}
	return false;
}
3.上传文件要限制文件的描述为MAXLEN个文字以内,QTextEdit
const int MAXLEN = 50;
void UpLoadDialog::slotTextChanged()
{
	QString textContent = m_text->toPlainText();
	int length = textContent.count();
	int maxLength = MAXLEN; 
	if(length > maxLength)
	{
		int position = m_text->textCursor().position();
		QTextCursor textCursor = m_text->textCursor();
		textContent.remove(position - (length - maxLength), length - maxLength);
		m_text->setText(textContent);
		textCursor.setPosition(position - (length - maxLength));
		m_text->setTextCursor(textCursor);
	}
}
4.下载文件保存路径的获取

void UpLoadDialog::slotDownSavePath()
{
	QString saveFileDir = QFileDialog::getExistingDirectory(this, tr("选择保存路径"), "/", QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
	if (saveFileDir.isEmpty())
	{
		return;
	}
	m_lineEditDownSavePath->setText(saveFileDir);//显示在一个QLineEdit上
	m_lineEditDownSavePath->setReadOnly(true);
}

5.获取一下本机操作者的基本信息,

//操作者基本信息获取
void UpLoadDialog::setHost()
{
	//返回主机名字
	m_strHostName = QHostInfo::localHostName();
	//通过名字创建QHostInfo对象.
	QHostInfo info = QHostInfo::fromName(m_strHostName);
	//QHostInfo对象下的地址
	QList<QHostAddress> addIPs = info.addresses();
	int i = 0;
	for(; i < addIPs.size(); i++)
	{
		if(addIPs[i].protocol() == QAbstractSocket::IPv4Protocol)
		{
			/////////////IP地址
			m_strIPv4 = addIPs[i].toString();
			break;
		}
	}
	//用户名
	m_strUserName = QDesktopServices::storageLocation(QDesktopServices::HomeLocation);
	m_strUserName = m_strUserName.section("/", -1, -1);
}
6.上传

//上传
void UpLoadDialog::slotOk()
{
	if (m_Path != NULL)
	{
		QString datetime = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss");//上传时间
		QString userName = m_strUserName;//上传用户
		if (!m_lineEditUpLoader->text().isEmpty())
		{
			userName = m_lineEditUpLoader->text();
		}
		QByteArray byte = compressFile(m_Path);//压缩文件
		if (!byte.isEmpty())
		{//和自己的数据库表保存的数据有关,保存什么上传什么东东.我的是用户名,用户IP,文件名,时间,压缩后的二进制文件
,文件描述			if(m_pMySQLManager->insertData(userName, m_strIPv4, m_filename, datetime, byte, m_text->toPlainText()))
			{
				updateData(); //更新数据
				m_lineEditPath->clear();
			}
		}
	}
	else
	{
		QMessageBox::warning(this, "警告", "上传文件不能为空!");
	}
}
7.下载

//下载
void UpLoadDialog::slotDown()
{
	QTreeWidgetItem* currentItem = m_treeWidget->currentItem();//在tree中当前选中的Item
	if (NULL != currentItem)
	{
		QString ID = currentItem->text(0);//选中的Item在数据库中的ID,这个是查询数据的时候要查询,并同时保存,在界面显示的时候隐藏
		QString name = currentItem->text(2);//获取文件名
		qDebug()<<"ID"<<ID;
		QString savedir = m_lineEditDownSavePath->text();//获取保存地址
		if (savedir.isEmpty())
		{
			QMessageBox::warning(this, "警告", "选择保存文件路径!");	
			return;
		}
		savedir.append("/");
		savedir.append(name);
		qDebug()<<"Savedir"<<savedir;
		if (!ID.isEmpty() && !savedir.isEmpty())
		{//调用m_pMySQLManager的函数,传入一个下载语句,downData()根据ID查询一条数据,保证唯一性.
			uncompressFile(savedir, m_pMySQLManager->downData(QString("select %1 from %2 where DATA_ID=%3").arg("DATA").arg("data").arg(ID)));
			m_lineEditDownSavePath->clear();
		}
	}
	else
	{
		QMessageBox::warning(this, "警告", "表格中选择将要下载的文件!");		
	}
}
8.在数据库中删除选中文件
//删除
void UpLoadDialog::slotDel()
{
	QTreeWidgetItem* currentItem = m_treeWidget->currentItem();//当前选择的Item
	if (NULL != currentItem)
	{
		QString ID = currentItem->text(0);//唯一ID
		if (!ID.isEmpty())
		{//输入语句
			m_pMySQLManager->delData(QString("DELETE FROM DATA WHERE DATA_ID=%1").arg(ID));
			qDebug()<<"delData() ID"<<ID;
			updateData();            //更新表格
		}
	}
	else
	{
		QMessageBox::warning(this, "警告", "表格中选择将删除的文件!");		
	}
}
9.更新界面,这个要在每次点击,插入新数据到数据库中,删除,或者是按照时间/上传者/文件名查询的时候更新,界面的表格,





你可能感兴趣的:(数据上传,下载(二))