Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] Upload Files with PUT into WS
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] Upload Files with PUT into WS

Scheduled Pinned Locked Moved General and Desktop
qnetworkreplyqnetworkrequest
6 Posts 2 Posters 1.6k Views 2 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.
  • GGAllinG Offline
    GGAllinG Offline
    GGAllin
    wrote on last edited by GGAllin
    #1

    Hi everyone
    I have a problem putting files to remote Web Services,
    here's the code i'm using

    void ApiV3Manager::uploadResource(QString btbid)
    {
    QString $url = QString("%1/%2").arg(DbSync::getInstance().getServer(), "api/v1/resources/"+btbid);

            QString path = btb::ResourceManager().getClipDir().absolutePath().append(+"/"+btbid+".png");
            qDebug() << "RESOURCE_PATH" << path;
            qDebug() << "URL" << $url;
            if(!QFileInfo(path).exists()){
                QList<btb::Annotation> a = btb::Annotation().findByBtbId(btbid);
                if(!a.isEmpty()){
                    path = a.at(0).getLink();
                    QUrl u = QUrl(path);
                    path = u.toLocalFile();
                    qDebug() << "PATH_HAS_CHANGED_IN" << path;
                }
            }
            QFile file(path);
            qDebug() << "FILE" << file.exists();
            if (!file.open(QIODevice::ReadWrite)) {
                qDebug() << "error read image";
                uploadError();
            return;
            }
            QNetworkRequest qnr($url);
            qnr.setAttribute(QNetworkRequest::CookieLoadControlAttribute,QNetworkRequest::Manual);
            qDebug() << "upload_resource_to" << $url;
            if(UserManager::getInstance().isLoggedIn())
            {
                qDebug() << "resource with session cookie";
                qnr.setRawHeader(QByteArray("X-Cookie"),
                                 QByteArray(UserManager::getInstance().getCurrentSession().toStdString().c_str()));
            }
            qnr.setHeader(QNetworkRequest::UserAgentHeader,ua_header_string);
            QNetworkReply* rep = NAMSingleton::getInstance().get()->put(qnr,file.readAll()); 
            QObject::connect(rep, SIGNAL(finished()), this,SLOT(onUploadFinished()));
    
    }
    

    QNetworkReply response i 200 OK but when i try to download it, is an invalid png and the size is about 40 byte
    Any idea ?
    thanks in advanced

    GGAllin ;-)

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Maybe a silly question but are you sure that the WS is working correctly ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      GGAllinG 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

        Maybe a silly question but are you sure that the WS is working correctly ?

        GGAllinG Offline
        GGAllinG Offline
        GGAllin
        wrote on last edited by
        #3

        @SGaist

        Hi SGaist,
        WS are working correctly, calling the same ws with android or flex application my files are uploaded (and downloaded) correctly...
        I'm sure i'm there is something wrong in my code, i'm sure i need to send png bytecode in body request, but 'im really newbie with qt,
        and i really don't know if my request contains a body with bytecode,

        this is the same call to ws written in Actionscript and work fine

        			var encodedFile:ByteArray = new PNGEncoder().encode(b.bitmapData)
        			
        			var request:URLRequest = new URLRequest(CloudService.CLOUD_RESOURCES+o.id);
        			request.method = URLRequestMethod.POST; 
        			var cookie:URLRequestHeader = new URLRequestHeader("X-Cookie",session);
        			var header:URLRequestHeader= new  URLRequestHeader("X-HTTP-Method-Override", "PUT"); // TO OVERRIDE POST METHOD
        			request.requestHeaders.push(cookie);
        			request.requestHeaders.push(header);
        			
        			
        			request.data = encodedFile;
        			var loader:URLLoader = new URLLoader();
        			loader.dataFormat = URLLoaderDataFormat.TEXT;
        			loader.addEventListener(Event.COMPLETE, loadSuccessHandler);
        			loader.addEventListener(IOErrorEvent.IO_ERROR, loadErrorHandler);
        			loader.addEventListener(IOErrorEvent.NETWORK_ERROR, loadErrorHandler);
        			loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, loadErrorHandler);
        			loader.load(request)
        

        I've also read that i can call put method with IODevice as second argument, but .. examples not found in the net :-/
        Need help! :-)
        Thanks

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          You just need to open the QFile and pass it to put

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • GGAllinG Offline
            GGAllinG Offline
            GGAllin
            wrote on last edited by
            #5

            Thanks SGaist!!

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              You're welcome !

              Since you have it working now, please update the thread title prepending [solved] so other forums users may know a solution has been found :)

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              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