Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send a file along with its filename over QTCPSocket?

Is there any simple way to send a file to server with the filename included so that the filename in server and client are exactly the same?

Here is my code

Sender

QString path = QApplication::applicationDirPath()+"/belajardansa.bmp";
QFile inputFile(path);
QByteArray read ;
inputFile.open(QIODevice::ReadOnly);
while(1)
{
    read.clear();
    read = inputFile.read(32768*8);
    qDebug() << "Read : " << read.size();
    if(read.size()==0)
       break;
    qDebug() << "Written : " << socket->write(read);
    socket->waitForBytesWritten();
    read.clear();
}
inputFile.close();

Receiver

QTcpSocket* socket = static_cast<QTcpSocket*>(sender());
QBuffer* buffer = buffers.value(socket);

QByteArray read = socket->read(socket->bytesAvailable());
qDebug() << "Read : " << read.size();

QFile file(  ???); // what should I put in the bracket???
if(!(file.open(QIODevice::Append)))
{
    qDebug("File cannot be opened.");
    exit(0);
}
file.write(read);
file.close();
like image 622
Ananta S. Avatar asked Dec 10 '12 12:12

Ananta S.


1 Answers

  1. You can create your own data structure that will represent file contents and its file name and convert it to QByteArray and vice versa.

  2. You can send two requests: the first with the file name and the second with data.

like image 91
hank Avatar answered Dec 28 '22 12:12

hank