Qt World Summit: Register Today!

[SOLVED] Upload Files with PUT into WS

  • 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;
                QList<btb::Annotation> a = btb::Annotation().findByBtbId(btbid);
                    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";
            QNetworkRequest qnr($url);
            qDebug() << "upload_resource_to" << $url;
                qDebug() << "resource with session cookie";
            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 ;-)

  • Lifetime Qt Champion


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

  • @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.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);

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

  • Lifetime Qt Champion

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

  • Thanks SGaist!!

  • Lifetime Qt Champion

    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 :)