Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. How to upload images / files from Qt quick desktop application ??
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved QML and Qt Quick
6 Posts 3 Posters 4.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    abcdCoder
    wrote on last edited by
    #1

    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 !

    1 Reply Last reply
    0
    • V Offline
      V Offline
      Vincent007
      wrote on last edited by
      #2

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

      1 Reply Last reply
      0
      • A Offline
        A Offline
        abcdCoder
        wrote on last edited by
        #3

        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@

        1 Reply Last reply
        0
        • A Offline
          A Offline
          abcdCoder
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          • p3c0P Offline
            p3c0P Offline
            p3c0
            Moderators
            wrote on last edited by
            #5

            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.

            157

            1 Reply Last reply
            0
            • V Offline
              V Offline
              Vincent007
              wrote on last edited by
              #6

              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

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved