Hi Danushka,
As per our chat you are sending base64 encoded string though a POST form
submission and read it inside Jaggery as request.getParameter("imageData")
. I believe following code snippet can help you.
var log = new Log();
// POST param 'imageData' should contain only base64 encoded string
var encodedImageString = request.getParameter("imageData"); //
encodedImageString is a string
try {
// decodedImageData is a byte array
var decodedImageData = Packages.javax.xml.bind.DatatypeConverter.
parseBase64Binary(encodedImageString);
} catch(e) {
log.error("Invalid base64 string");
log.error(e);
return;
}
try {
// file is written in {CARBON_PRODUCT_HOME}/myimage.png
var ofStream = new Packages.java.io.FileOutputStream("myimage.png"); //
assuming image type is PNG
ofStream.write(decodedImageData);
ofStream.close();
log.info("Content written successfully.");
} catch (e) {
log.error("Could not write to file.");
log.error(e);
}
Above, base64 encoded string is decoded using JABX package [1] (assuming
you are using Java6). Some other options can be found here [2].
[1]
http://docs.oracle.com/javase/6/docs/api/javax/xml/bind/DatatypeConverter.html#parseBase64Binary%28java.lang.String%29
[2] http://stackoverflow.com/questions/469695/decode-base64-data-in-java
Thanks.
On Wed, Sep 23, 2015 at 2:46 PM, Danushka Fernando <[email protected]>
wrote:
> Hi All
> Appreciate answers. Any way what I am looking for in write base64 encoded
> image binary data to a file. And these answers won't satisfy my answers.
> Initially I checked for the options available in jaggery to cater my
> requirement. But after the chat had with Manu it seems that Jaggery have
> some problem with the requirement and they are working on it.
> Any way I tried to do it from the java script and I used the following
> code. Still I have no idea how to write this blob to a File with jaggery.
> Manu can you check this?
>
> var data = atob(imageData.replace(/^data:image\/\w+;base64,/, ""));
> console.log(data);
> var uint8Array = new Uint8Array(data.length);
> for( var i = 0, len = data.length; i < len; ++i ) {
> uint8Array[i] = data.charCodeAt(i);
> }
> var blob = new Blob( [ uint8Array.buffer ], {type: "image/jpg"} );
>
> Thanks & Regards
> Danushka Fernando
> Senior Software Engineer
> WSO2 inc. http://wso2.com/
> Mobile : +94716332729
>
> On Wed, Sep 23, 2015 at 11:17 AM, Sajith Ariyarathna <[email protected]>
> wrote:
>
>> Hi Madusanka,
>>
>> The code snippet you mentioned [1] *does not work* in Jaggery as it
>> meant to be run in the client-side.
>>
>> [1]
>> https://github.com/wso2/carbon-apimgt/blob/carbon-apimgt-v1.9.0/features/apimgt/org.wso2.carbon.apimgt.publisher.feature/src/main/resources/publisher/site/themes/default/templates/item-design/js/api-design.js#L681
>>
>>
>> Hi Dhanushka,
>>
>> You can use File object [2] in Jaggery to read, write files. If you are
>> not satisfied with it, you can always use Java classes in your Jaggery code.
>>
>> var log = new Log();
>>
>> var content = "This is the content to write into file";
>> try {
>> var file = new Packages.java.io.File("myfile.txt"); // path to
>> file
>> if (!file.exists()) {
>> log.info("File '" + file.getAbsolutePath() + "' does not
>> exists.");
>> file.createNewFile();
>> log.info("File '" + file.getAbsolutePath() + "' created.");
>> }
>>
>> var fw = new Packages.java.io.FileWriter(file);
>> var bw = new Packages.java.io.BufferedWriter(fw);
>> bw.write(content);
>> bw.close();
>>
>> log.info("Content written successfully.");
>> } catch (e) {
>> log.error("Could not write to file.");
>> log.error(e);
>> }
>>
>>
>> [2] http://jaggeryjs.org/documentation.jag?api=file
>>
>> Thanks.
>>
>> On Tue, Sep 22, 2015 at 12:17 PM, Madusanka Premaratne <
>> [email protected]> wrote:
>>
>>> Hi Dhanushka,
>>> In APIM we used FileReader[1] to read imported file from file system. I
>>> think you can use the file writer[2] in your case.
>>>
>>> [1] -
>>> https://github.com/wso2/carbon-apimgt/blob/carbon-apimgt-v1.9.0/features/apimgt/org.wso2.carbon.apimgt.publisher.feature/src/main/resources/publisher/site/themes/default/templates/item-design/js/api-design.js#L681
>>> [2] - http://www.w3.org/TR/file-writer-api/
>>>
>>> Thanks,
>>> Madusanka
>>>
>>> On Tue, Sep 22, 2015 at 11:55 AM, Danushka Fernando <[email protected]>
>>> wrote:
>>>
>>>> BTW I tried using Blob and Uint8Array interfaces but jaggery says they
>>>> are not identified.
>>>>
>>>> Thanks & Regards
>>>> Danushka Fernando
>>>> Senior Software Engineer
>>>> WSO2 inc. http://wso2.com/
>>>> Mobile : +94716332729
>>>>
>>>> On Tue, Sep 22, 2015 at 11:50 AM, Danushka Fernando <[email protected]
>>>> > wrote:
>>>>
>>>>> Hi All
>>>>> How we can do $subject? Any ideas would be appreciated.
>>>>>
>>>>> Thanks & Regards
>>>>> Danushka Fernando
>>>>> Senior Software Engineer
>>>>> WSO2 inc. http://wso2.com/
>>>>> Mobile : +94716332729
>>>>>
>>>>
>>>>
>>>> _______________________________________________
>>>> Dev mailing list
>>>> [email protected]
>>>> http://wso2.org/cgi-bin/mailman/listinfo/dev
>>>>
>>>>
>>>
>>>
>>> --
>>> *Madusanka Premaratne* | Associate Software Engineer
>>> WSO2, Inc | lean. enterprise. middleware.
>>> #20, Palm Grove, Colombo 03, Sri Lanka
>>> Mobile: +94 71 835 70 73| Work: +94 112 145 345
>>> Email: [email protected] | Web: www.wso2.com
>>>
>>> [image: Facebook] <https://www.facebook.com/maduzanka> [image: Twitter]
>>> <https://twitter.com/rmmpremaratne> [image: Google Plus]
>>> <https://plus.google.com/u/0/+MadusankaPremaratnemaduz/about/p/pub> [image:
>>> Linkedin] <http://lk.linkedin.com/in/madusanka/> [image: Instagram]
>>> <http://instagram.com/madusankapremaratne> [image: Skype]
>>> <http://@rmmpremaratne>
>>>
>>>
>>> _______________________________________________
>>> Dev mailing list
>>> [email protected]
>>> http://wso2.org/cgi-bin/mailman/listinfo/dev
>>>
>>>
>>
>>
>> --
>> Sajith Ariyarathna
>> Software Engineer; WSO2, Inc.; http://wso2.com/
>> mobile: +94 77 6602284, +94 71 3951048
>>
>
>
> _______________________________________________
> Dev mailing list
> [email protected]
> http://wso2.org/cgi-bin/mailman/listinfo/dev
>
>
--
Sajith Ariyarathna
Software Engineer; WSO2, Inc.; http://wso2.com/
mobile: +94 77 6602284, +94 71 3951048
_______________________________________________
Dev mailing list
[email protected]
http://wso2.org/cgi-bin/mailman/listinfo/dev