Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Contributor
Posts: 28
Registered: ‎02-07-2013
My Device: blackberry 10
My Carrier: vodafone

sending file from cascade native to web server

What is the best way to send a file from native cascade application to webserver ?

Developer
Posts: 1,524
Registered: ‎12-18-2012
My Device: Z30, Z10 LE, DevAlpha C, PlayBook

Re: sending file from cascade native to web server

[ Edited ]

Hi,

 

This can be done using QNetworkAccessManager:

http://stackoverflow.com/questions/2806274/uploading-a-file-using-post-method-of-qnetworkaccessmanag...

 

Or if you need more control this example shows how to construct the body manually:

http://qt-project.org/forums/viewthread/11361

 

I think it's better to ask in Cascades forum.

 


Andrey Fidrya, @zmeyc on twitter
Contributor
Posts: 28
Registered: ‎02-07-2013
My Device: blackberry 10
My Carrier: vodafone

Re: sending file from cascade native to web server

I have tried uploading a file using  the  QNetworkAccessManager ! There is no error! But its not uploading! Any help will be apperciated!

Highlighted
Developer
Posts: 526
Registered: ‎05-17-2009
My Device: 9900
My Carrier: ATT

Re: sending file from cascade native to web server

Do you have snippets of the code youre running? Are you sure that the file path is correct?

Like all of my posts
Developer
Posts: 1,008
Registered: ‎12-12-2010
My Device: Passport (Red Limited Edition)
My Carrier: Mobile Vikings

Re: sending file from cascade native to web server

You may want to show some client side code (and potentially some server side code as well).

-------------------------------------------
BlackBerry Certified Builder for Native Application Development -- Proud member of the Belgian BlackBerry Developer group
Samples: Park in Ghent
Feeling generous? Nominate me for BB Elite member!
Contributor
Posts: 28
Registered: ‎02-07-2013
My Device: blackberry 10
My Carrier: vodafone

Re: sending file from cascade native to web server

[ Edited ]

Hi I am using this code to upload the file at client side,

 

   
       QString bound="margin";
    
               const QUrl url(" upload.php");
    QString _DB_FILE("/accounts/1000/shared/downloads/hello.jpg");
 
        QFile file(_DB_FILE);
        file.open(QIODevice::ReadOnly);
         QByteArray data;
        QUrl params;

        QString userString("hello.jpg");
        QString param1("name");
        QString param2("image");

        params.addQueryItem(param1, userString );
   
        params.addQueryItem(param2,file.readAll());
        data.append(params.toString());
           
                if (!file.open(QIODevice::ReadOnly))
                    return;
        
   QNetworkRequest request(url);
      if (AppSettings::isUsingHttps()) {
            request.setUrl(QUrl("upload.php"));

            QSslConfiguration config = request.sslConfiguration();
            config.setPeerVerifyMode(QSslSocket::VerifyNone);
            config.setProtocol(QSsl::TlsV1);
            request.setSslConfiguration(config);
        }
        QNetworkAccessManager m_networkAccessManager;
        QNetworkReply* reply = m_networkAccessManager->post(request, data);
        connect(reply, SIGNAL(finished()), this, SLOT(onGetReply()));

 

and in my server side i use this code!

 

<?php
     $img = $_POST['image'];
        $name = $_POST['?name'];
 
echo $name;
echo $img;
    $file_location = $_FILES['image']['?name'];      
    $dir_file = "/" . basename($_FILES['image']['?name']);
    if(move_uploaded_file($file_location,"$dir_file")) {
        echo "File was uploaded";
    }
    else{
        echo "File was not uploaded";
    }    
 $File = "hello.jpg";
 $Handle = fopen($File, 'w');
 
 fwrite($Handle, $img);
 
 print "Data Written";
 fclose($Handle);

?>

 

Now the uploading is working ! But file.readAll() not reading all content! I think, data.append() will work! But while doing so, data is not receiving at server side!

Developer
Posts: 1,524
Registered: ‎12-18-2012
My Device: Z30, Z10 LE, DevAlpha C, PlayBook

Re: sending file from cascade native to web server

[ Edited ]

Hi!

The post() call is asynchronous, it won't send the data instantly.

You're passing a reference to local variable to post() which is destroyed before all data is sent.

Make the data a member variable and ensure it exists until finished() signal is received.

 

This is the same issue that was described in this StackOverflow post:

http://stackoverflow.com/questions/2806274/uploading-a-file-using-post-method-of-qnetworkaccessmanag... Thanks Kyle!

 

Btw, you're trying to open a file twice:

QFile file(_DB_FILE);
file.open(QIODevice::ReadOnly);

 

 

                if (!file.open(QIODevice::ReadOnly))
                    return;

 

It seems to me that PHP script has errors in file_location part.

 


Andrey Fidrya, @zmeyc on twitter
Developer
Posts: 526
Registered: ‎05-17-2009
My Device: 9900
My Carrier: ATT

Re: sending file from cascade native to web server

The post data should be fine since hes using a QByteArray and not the file reference. That will copy over just fine and be uploaded correctly.

Zmey what you pointed out with opening twice might cause the issue. Wouldnt trying to open it twice cause that check to fail and return get called before anything is even posted?

Also have you used qDebug() to verify that your data actually contains what you want it to before sending?
Like all of my posts