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
Forums Advisor I
Posts: 501
Registered: ‎03-01-2013
My Device: BlackBerry Z10

Http post

Is it possible to send image through http post method??

If yes,how?? and if no then why??

 

 

Developer
Posts: 16,992
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Http post

yes.
enter "post" in the search field to review some resolved questions about that.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Forums Advisor I
Posts: 501
Registered: ‎03-01-2013
My Device: BlackBerry Z10

Re: Http post

I have seen examples of posting text only.then how do i send image using http post??

Developer
Posts: 16,992
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Http post

you convert the image to text (which is called base64 encoding).
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Forums Advisor I
Posts: 501
Registered: ‎03-01-2013
My Device: BlackBerry Z10

Re: Http post

[ Edited ]

Hey see

I have made a button in qml
 and when i click on it the image that i have passed through QHttpMultiPart is uploaded to server and server responses success if uploaded otherwise error.

Here is my code:

main.qml



import bb.cascades 1.0



        NavigationPane {
            id: httpPostNavPane

            onPopTransitionEnded: page.destroy()

            Page {
                Container {
                    layout: DockLayout {}

                    // The background image
                    ImageView {
                        horizontalAlignment: HorizontalAlignment.Fill
                        verticalAlignment: VerticalAlignment.Fill

                        imageSource: "asset:///images/background.png"
                    }

                    Container {
                        horizontalAlignment: HorizontalAlignment.Fill
                        verticalAlignment: VerticalAlignment.Top

                        background: Color.Black

                        Label {
                            horizontalAlignment: HorizontalAlignment.Center

                            text: qsTr("POST REQUESTS")
                            textStyle {
                                base: SystemDefaults.TextStyles.BigText;
                                color: Color.White
                                fontStyle: FontStyle.Italic
                                fontWeight: FontWeight.Bold
                                textAlign: TextAlign.Center
                            }
                        }
                    }
                    //! [2]
                    Container {
                        verticalAlignment: VerticalAlignment.Center
                        leftPadding: 30
                        rightPadding: 30

                    

                        Button {
                            horizontalAlignment: HorizontalAlignment.Center

                            text: qsTr("Post!")

                            onClicked: {
                             
                                httpPostNavPane.push(postPageDefinition.createObject());
                            }

                            attachedObjects: ComponentDefinition {
                                id: postPageDefinition
                                source: "post.qml"
                            }
                        }
                    }
                }
            }
        }

 

post.qml

 

/* Copyright (c) 2012 Research In Motion Limited.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import bb.cascades 1.0
import Network.PostHttp 1.0
import "controls"
import my.library 1.0

/**
*  This is a simple Page that allows you to enter HTML form values and POST them to a newtwork resource
*
*  You will learn how to:
*  -- Import C++ objects
*  -- Use attachedObjects
*  -- Use a QTimer for timing operations
**/
Page {
    Container {

        layout: DockLayout {}

        // The background image
        ImageView {
            horizontalAlignment: HorizontalAlignment.Fill
            verticalAlignment: VerticalAlignment.Fill

            imageSource: "asset:///images/background.png"
        }
        //! [0]
        Container {
            horizontalAlignment: HorizontalAlignment.Center
            verticalAlignment: VerticalAlignment.Center
            leftPadding: 30

            Label {
                id: postResponseLabel
                visible: false

                text: qsTr("Response:")
                textStyle {
                    base: SystemDefaults.TextStyles.BodyText;
                    color: Color.White
                }
            }

            TextArea {
                id: postResponseBody
                visible: false
                editable: false
                backgroundVisible: false

                textStyle {
                    base: SystemDefaults.TextStyles.BodyText;
                    color: Color.White
                }
            }
        }

        NetworkActivity {
            id: progressIndicator

            horizontalAlignment: HorizontalAlignment.Fill
            verticalAlignment: VerticalAlignment.Fill

            title: qsTr("Posting...")
        }

        attachedObjects: [
            QTimer {
                id: timer
                interval: 1000
                onTimeout: {
                    // One second after page loads make the http post request
                  //  netpost.post(_httpsample.postBody);
                    netpost.post();
                }
            },
            PostHttp {
                id : netpost
                onComplete :{
                    progressIndicator.active = false;
                    progressIndicator.visible = false;

                    postResponseBody.text = info;
                    postResponseBody.visible = true;
                    postResponseLabel.visible = true;

                    timer.stop();
                }
            }
        ]

        onCreationCompleted: {
            progressIndicator.active = true;
            timer.start();
        }
        //! [0]
    }
}

 

post method in C++ file

 

QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
    QHttpPart imagePart;
    imagePart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("image/png"));
    imagePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"image\""));
    QFile *file = new QFile("1.png");
    file->open(QIODevice::ReadOnly);
    imagePart.setBodyDevice(file);
    file->setParent(multiPart); // we cannot delete the file now, so delete it with the multiPart

 //   multiPart->append(textPart);
    multiPart->append(imagePart);
    QUrl url("some url here..");
    QNetworkRequest request(url);
    QNetworkAccessManager manager;
    QNetworkReply *reply = manager.post(request, multiPart);
    multiPart->setParent(reply);


    connect(reply, SIGNAL(finished()), this, SLOT(onGetReply()));
but this is not working.

 

 

Forums Advisor I
Posts: 501
Registered: ‎03-01-2013
My Device: BlackBerry Z10

Re: Http post

Hey

I hav etried a lot but still my image is not uploading on the server. Everytime i run this it shows file uploading error.

Can anyone tell me the problem???

 

Here is the code...

 

    const QUrl url("link");
    QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
    QHttpPart imagePart;
    imagePart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("image/jpg"));
    imagePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"image\""));
    QFile *file = new QFile("/accounts/1000/shared/camera/Pics/1.JPG");
    file->open(QIODevice::ReadOnly);
    imagePart.setBodyDevice(file);
    file->setParent(multiPart);


    multiPart->append(imagePart);

    QNetworkRequest request(url);

    QNetworkReply *reply = m_networkAccessManager->post(request, multiPart);
    multiPart->setParent(reply);


    connect(reply, SIGNAL(finished()), this, SLOT(onGetReply()));

Forums Advisor I
Posts: 501
Registered: ‎03-01-2013
My Device: BlackBerry Z10

Re: Http post

Is there any other way to upload image to server?????

Any reply???????????????????