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

Java Development

Reply
Developer
Posts: 81
Registered: ‎07-16-2008
My Device: Curve - 8320

Using MultipartPostData to Upload an Image ...

Hello Everyone,

I am sure, this is not some thing new that I am trying for the first time. This would be tried by people 'n' number of times before me. Please help regarding how to use MultipartPostData to upload binary as well as text data.

 

I am trying to use MultipartPostData to submit a page that contains a JPG image, a WAV audio and some normal text field like- Name, Address, City etc. The class only has some methods like append(). Using the append method, I have prepared the form and submitted, which at the server side looks like this-


===============================================
--------------1240530863604
Content-Disposition: form-data; name="DT-REQST-refID"

610
--------------1240530863604
Content-Disposition: form-data; name="DT-RESPN-Code"

REQ_SSHT
--------------1240530863604
Content-Disposition: form-data; name="DT-DATA-Type"

BIN
--------------1240530863604
Content-Disposition: form-data; name="DT-IMG-DATA"

<Screenshot_BINARY_DATA>
--------------1240530863604
Content-Disposition: form-data; name="DT-AUD-DATA"

<WaveAudio_BINARY_DATA>
--------------1240530863604--
===============================================

I wonder if the above said class doesn't have any methods to support setting the form name, or setting the content-types for IMAGE and WAV parts of the forms. The posted form sets the content type of even the binary parts to text, which is not true. I am using the following code snippet to prepare my Multipart Form:

    PostData form = new MultipartPostData(MultipartPostData.DEFAULT_CHARSET, false) ;
    form.append(REQUEST_REF_ID, _id);
    form.append(RESPONSE_ID, _responseCode);
    form.append(DATA_TYPE, _type);
    if (_data != null && _data.size() > 0)  {
            Enumeration enKey = _data.keys();
            if (enKey != null) {
                while (enKey.hasMoreElements()) {
                    Object objThisKey = enKey.nextElement();
                        form.append(objThisKey.toString(), _data.get(objThisKey).toString()) ;
                }
            }
    }

  try {
      conn = (HttpConnection) Connector.open(url);
      conn.setRequestMethod(HttpConnection.POST);
      conn.setRequestProperty(HttpProtocolConstants.HEADER_CONTENT_LENGTH, String.valueOf(postData.length));
      conn.setRequestProperty("Content-Language", "en-US");
      out = conn.openOutputStream();
      out.write( form.getBytes() );
      statusCode = conn.getResponseCode();
    } catch (Throwable t) {
      // LOG here
    }


Is there any example on the web, that shows how to use MultipartPostData to post any binary data ?   It is strange that the class is not enough documented regarding is use, and also not any sample is found so far regarding uploading binary data using this class.

 

Any help is greatly appreciated.

 

Thanks,

*(Vipul)() ;

*(Vipul)() ;
BlackBerry Development Advisor
Posts: 15,021
Registered: ‎07-09-2008
My Device: BlackBerry Passport

Re: Using MultipartPostData to Upload an Image ...

You can use the MultipartPostData.setData method to add binary data to the post request.  MultipartPostData.append is designed for text data.
Mark Sohm
BlackBerry Development Advisor

Please refrain from posting new questions in solved threads.
Problem solved? Click the Accept As Solution button.
Found a bug? Report it using Issue Tracker
Developer
Posts: 81
Registered: ‎07-16-2008
My Device: Curve - 8320

Re: Using MultipartPostData to Upload an Image ...

Thanks Mark,

 

But I guess it can only set one binary data as calling setData() again would replace  the previously set data. Actually, I need to send a image and a WAV file in one HTTP post. Please let me, know how to achieve that using MultipartPostData.

 

Thanks,

 

*(Vipul)() ;
Developer
Posts: 49
Registered: ‎06-02-2009
My Device: Curve8530, Curve8900, Storm9500

Re: Using MultipartPostData to Upload an Image ...

In HTML to upload a file we have to specifiy the name of file field (file name field is used in php to get file). but here how can we specify it

 

<form action="http://myserver/post.php" enctype ="multipart/form-data" method="post">
<input type="file" name="uploadedfile">
<input type="submit">
</form>

 

Please view a example in below link. example code i have used on BB and working fine for upload a file.

http://developer.sonyericsson.com/site/global/techsupport/tipstrickscode/java/p_post_javafile_webser...