How to upload images / files from Qt quick desktop application ??



  • Most of the solutions out there are couple of years old(QNetworkAccessManager ). I was wondering if we have better and simpler ways to upload files to the server. Kindly help. Thanks !



  • Is it difficult to upload files by QNetworkAccessManager?
    You can use xmlhttprequest in QtQuick.



  • Hi Can you paste me snippet and what needs to be done ? I tried a lot with QNetworkAccessManager and hitting roadblocks.
    UPLOAD.H
    @#ifndef UPLOAD_H
    #define UPLOAD_H

    #endif // UPLOAD_H

    #include <QtGui>

    class upload : public QObject{
    Q_OBJECT
    public:
    explicit upload (QObject* parent = 0) : QObject(parent) {}
    Q_INVOKABLE void processFile(){

        }
    
    QByteArray buildUploadString();
    

    };
    @

    UPLOAD.CPP
    @#include "Upload.h"

    void upload::processFile()
    {
    QByteArray postData;
    //Look below for buildUploadString() function
    postData = mReport->buildUploadString();

    QUrl mResultsURL = QUrl("http://" + VariableManager::getInstance()->getServerIP() + "/uploadFile.php");
    QNetworkAccessManager* mNetworkManager = new QNetworkAccessManager(this);

    QString bound="margin"; //name of the boundary

    QNetworkRequest request(mResultsURL); //our server with php-script
    request.setRawHeader(QString("Content-Type").toAscii(),QString("multipart/form-data; boundary=" + bound).toAscii());
    request.setRawHeader(QString("Content-Length").toAscii(), QString::number(postData.length()).toAscii());

    connect(mNetworkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(printScriptReply(QNetworkReply*))); //This slot is used to debug the output of the server script
    mNetworkManager->post(request,postData);
    }

    QByteArray upload::buildUploadString()
    {
    QString path = VariableManager::getInstance()->getReportDirectory();
    path.append("\\");
    path.append(getReportFileName());

    QString bound="margin";
    QByteArray data(QString("--" + bound + "\r\n").toLatin1());
    data.append("Content-Disposition: form-data; name=\"action\"\r\n\r\n");
    data.append("uploadFile.php\r\n");
    data.append(QString("--" + bound + "\r\n").toLatin1());
    data.append("Content-Disposition: form-data; name=\"uploaded\"; filename=\"");
    data.append(getReportFileName());
    data.append("\"\r\n");
    data.append("Content-Type: text/xml\r\n\r\n"); //data type
    
    QFile file&#40;path&#41;;
        if (!file.open(QIODevice::ReadOnly)){
            qDebug() << "QFile Error: File not found!";
            return data;
        } else { qDebug() << "File found, proceed as planned"; }
    
    data.append(file.readAll());
    data.append("\r\n");
    data.append("--" + bound + "--\r\n");  //closing boundary according to rfc 1867
    
    
    file.close();
    
    return data;
    

    }
    @
    MAIN.CPP
    @#include <QtGui/QGuiApplication>
    #include "qtquick2applicationviewer.h"
    #include "upload.h"
    #include <QtQuick>

    int main(int argc, char *argv[])
    {
    QGuiApplication app(argc, argv);

    qmlRegisterType<upload>("com.myself", 1, 0, "upload");
    
    QtQuick2ApplicationViewer viewer;
    viewer.setMainQmlFile&#40;QStringLiteral("qml/RealEstate/main.qml"&#41;);
    viewer.showMaximized();
    viewer.setMinimumHeight(700);
    viewer.setMinimumWidth(1200);
    return app.exec();
    

    }
    @
    PRO file
    @# Add more folders to ship with the application, here
    folder_01.source = qml/RealEstate
    folder_01.target = qml
    DEPLOYMENTFOLDERS = folder_01

    QT_DISABLE_DEPRECATED_BEFORE=0x000000

    Additional import path used to resolve QML modules in Creator's code model

    QML_IMPORT_PATH =

    QT += network
    declarative

    If your application uses the Qt Mobility libraries, uncomment the following

    lines and add the respective components to the MOBILITY variable.

    CONFIG += mobility

    MOBILITY +=

    The .cpp file which was generated for your project. Feel free to hack it.

    SOURCES += main.cpp
    Upload.cpp

    Installation path

    target.path =

    Please do not modify the following two lines. Required for deployment.

    include(qtquick2applicationviewer/qtquick2applicationviewer.pri)
    qtcAddDeployment()

    RESOURCES +=
    resources.qrc

    OTHER_FILES +=
    Storage.js

    HEADERS +=
    Upload.h@



  • I get following build errors -
    C:\Qt\RealEstate\Upload.cpp:4: error: redefinition of 'void upload::processFile()'
    void upload::processFile()
    ^
    C:\Qt\RealEstate\Upload.h:13: error: 'void upload::processFile()' previously defined here
    Q_INVOKABLE void processFile(){
    ^
    C:\Qt\RealEstate\Upload.cpp:27: error: 'VariableManager' has not been declared
    QString path = VariableManager::getInstance()->getReportDirectory();
    ^

    C:\Qt\RealEstate\Upload.cpp:29: error: 'getReportFileName' was not declared in this scope
    path.append(getReportFileName());
    ^

    Are these issues due to this bug - https://bugreports.qt-project.org/browse/QTBUG-31064


  • Moderators

    Hi,

    There are some issues in your code.
    First there's redefinition of processFile() in the header as well as cpp file. Just declare it in header and implement in the source file.
    Second there's no VariableManager. Is it a class ? You need to include its header file.
    Third there's no definition of getReportFileName() in your code and hence the error.

    Apart from that why don't you use FTP protocol for your file uploading ?
    You can use vsftpd or similar for linux and there are Ftp servers for windows too.



  • Sorry, I don't have.

    But you can refer to "Qt Qnetworkaccessmanager can't upload to local FTP server":http://stackoverflow.com/questions/17755498/qt-qnetworkaccessmanager-cant-upload-to-local-ftp-server
    "QNetworkAccessManager uploading files":http://qt-project.org/forums/viewthread/11361


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.