Search code examples
qtubuntufile-uploadftpftp-server

Unsuccessful upload to a ftp server on Linux using Qt![File zero size at destination]


I am trying to upload a file to a FTP server, which is a local server created by vsftpd. I have set necessary parameters needed for connecting and transferring files in the vsftpd.conf file. My requirement is to upload a file to this server. When i logged statechanged messages, HostLookup, Connecting, Connected, Logged in, closing, and Unconnected messages were emitted by my ftp object. But when i check in the destination directory the file is there but of 0 size...What could be wrong? following is the code I used...

QImage img("./Sample.jpg");
QBuffer* buf = new QBuffer();
buf->open(QBuffer::ReadWrite);
buf->seek(0);
img.save(buf, "jpg");
connection = new QFtp();
connection->connectToHost("localhost");
connection->login();
connection->cd("ftpshare/");
connection->put(buf, "Sample.jpg", QFtp::Binary);
qDebug(QString::number(connection->error()).toLatin1());
qDebug(connection->errorString().toLatin1());
connect(connection,SIGNAL(stateChanged(int)),this,SLOT(ftpstatechanged(int)));
connection->close();

Solution

  • The problem was with usage of buffer. It got solved when I used QByteArray instead.

    QImage img("./Sample.png");
    QByteArray ba;
    QBuffer buffer(&ba);
    buffer.open(QIODevice::WriteOnly);
    img.save(&buffer, "PNG");
    connection = new QFtp();
    connection->connectToHost("localhost");
    connection->login();
    connection->cd("ftpshare/");
    connection->put(ba, "Sample.png", QFtp::Binary);