How consume the ArrayBuffer/ByteArray http response using nativescript filesystem


#1

Hey all!

Actually my API server is gives the response type of byte array/array buffer data for http post call.

So in my nativescript with angular application, i need to write the byte array/array buffer response via http post call into the .zip file using filesystem. That actual byte array response contains around 50mb - 100 mb of datas.

Http post call :

 let headers = new Headers();
    headers.append("Content-Type", "application/json");

    this.http.post(this.postserverUrl + "***/***/***/" + ***+ "/***" + "/**" + "/*", { headers: headers })
        .map(result => result.json() )
        .do(result => console.log("do RESULT: " + JSON.stringify(result)))
        .subscribe(result => {
            if (result) {
                console.log("Final data " + JSON.stringify(result["_body"]));

                let documents = fs.knownFolders.documents();
                let path = fs.path.join(documents.path, "602024.zip");
                let contents = result["_body"];
                let file = fs.File.fromPath(path);

                file.writeSync(contents, (e) => {
                    console.log(e);
                });
                console.log(file);
            }
            else if (result.arrayBuffer().byteLength == 0) {
                console.log("There is no byte datas");
            }
        }, error => {
            console.log("ERROR: ", error);
        });
}

Http post call response :

JS: hitted.. JS: do RESULT: {"_body":"PK\u0003\u0004\u0014\u0000\b\b\b\u0000\u0001��K\u0000\u0000\u0000\u0000\u0000\u0000\u000 0\u0000\u0000\u0000\u0000\u0000\u000b\u0000\u0000\u0000602024.json�T]o�0\u0014�+�\u001f�MJ\"\fIH�4\u0006T���\n��0 ��X\u0005�l�]T��\u0006���Ҵ-�\u00139��|�{�3��\u0005�\t�\u00161x9�l˞�Ϊ&�\u000f�����H\u000f\r�ߑe�\u0011|$B�\u0012WL
�\n9����iL�_���\t���$�\u001b.\u0014:_̠�\u000f\u0014�P�B�\u0018�cۆ'�\u001f��5C\u000b�T\bX��u\f.�b�G�Dfr/N�-\u0018�
�q�+��\u0005;OX���&�%�Y�\r<E\u0013ob���\r���u1��c\u001b\r�^\u000b�\u001aRo\b�����h����]ږ�\u001f�L��\u001a���c\u00
1a�H��\u0007�U�d�C\u0017V!#�\u0017��Z=Ç�1�c\n���=\u0015Ҙr:QsSn\n����&�04ۊ�\u0005��d�䑒����/$��=��\f�ہh:/�W85h;ă
B��\u001f;H���nw&a@�^,\u0015����@s�4�s\u0002���\u0001�#���\n�j\u0001�\u001dHH.�3@Ӊ5\u0002�\u0019���u'N6�g\u0005\u
0001��\u0018\u0007X/�V����f{�F\u0013Gi\u0018�MW��R<ޙ����&��08V�h]�u��/�\u0006iU\u0019\u000f��h���nc�f��\u00189�eO
�/bw�$8\u0018@k�\u0015经��ov�)��\nl��x��IB8�&��\u00048����?n�'e���좋��5q�T��\u0017��CJyb\u001d\u0006\u0001�k/�/�޸} N���\u0018O�0\n<u���t��9�{�M��a��+�{i\b�֧�>}\u001b�q�ٌ\\u001d�\u0010M��a�o���\u000fPK\u0007\b�1\�t\u0002\u0000
\u0000q\u0007\u0000\u0000PK\u0001\u0002\u0014\u0000\u0014\u0000\b\b\b\u0000\u0001��K�1\�t\u0002\u0000\u0000q\u00
07\u0000\u0000\u000b\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0
000\u0000602024.jsonPK\u0005\u0006\u0000\u0000\u0000\u0000\u0001\u0000\u0001\u00009\u0000\u0000\u0000�\u0002\u000
0\u0000\u0000\u0000","status":200,"ok":true,"statusText":"OK","headers":{"null":["HTTP/1.1 200 OK"],"Access-Contr
ol-Allow-Credentials":["true"],"Access-Control-Allow-Headers":["X-Access-Token"," X-Requested-With"," pragma","Co
ntent-Type"," Authorization"," Origin"," Accept"," Cache-Control","Access-Control-Request-Method"," Access-Contro
l-Request-Headers"],"Access-Control-Allow-Methods":["POST"," GET"," PUT"," OPTIONS"," DELETE"],"Access-Control-Al
low-Origin":[""],"Access-Control-Max-Age":["3600"],"Cache-Control":["no-cache"," no-store"," max-age=0"," must-r
evalidate"],"Content-Disposition":["form-data; name="602024.zip"; filename="602024.zip""],"Content-Length":["
764"],"Content-Type":["application/zip"],"Date":["Fri"," 22 Dec 2017 15:54:03 GMT"],"Expires":["0"],"Pragma":["no
-cache"],"Server":["GlassFish Server Open Source Edition 4.1.2"],"X-Android-Received-Millis":["1513958247199"],"
X-Android-Response-Source":["NETWORK 200"],"X-Android-Selected-Transport":["http/1.1"],"X-Android-Sent-Millis":["
1513958247085"],"X-Content-Type-Options":["nosniff"],"X-Frame-Options":["DENY"],"X-Powered-By":["Servlet/3.1 JSP/
2.3 (GlassFish Server Open Source Edition 4.1.2 Java/Oracle Corporation/1.8)"],"X-XSS-Protection":["1; mode=blo
ck"]},"type":2,"url":"*********************"}

Actually arraybuffer response contains the .zip file named as 602024.zip. As below located in http response,

"Content-Disposition":["form-data; name=\"602024.zip\"; filename=\"602024.zip\""],"Content-Length":[" 764"],

This is how! I am trying to write the arraybuffer response into filesystem,

let documents = fs.knownFolders.documents();
let path = fs.path.join(documents.path, "602024.zip");
let contents = result["_body"];
let file = fs.File.fromPath(path);

                file.writeSync(contents, (e) => {
                    console.log(e);
                });
                console.log(file);
            }`

If i tried to writeSync then i got below mentioned error!

JS: Error: java.lang.Exception: Failed resolving method write on class java.io.FileOutputStream JS: com.tns.Runtime.resolveMethodOverload(Runtime.java:1049) JS: com.tns.Runtime.callJSMethodNative(Native Method) JS: com.tns.Runtime.dispatchCallJSMethodNative(Runtime.java:1088) JS: com.tns.Runtime.callJSMethodImpl(Runtime.java:970) JS: com.tns.Runtime.callJSMethod(Runtime.java:957) JS: com.tns.Runtime.callJSMethod(Runtime.java:941) JS: com.tns.Runtime.callJSMethod(Runtime.java:933) JS: com.tns.gen.org.nativescript.widgets.Async_CompleteCallback.onComplete(Async_CompleteCallback.java:12) JS: org.nativescript.widgets.Async$Http$HttpRequestTask.onPostExecute(Async.java:585) JS: org.nativescript.widgets.Async$Http$1$1.run(Async.java:486) JS: android.os.Handler.handleCallback(Handler.java:733) JS: android.os.Handler.dispatchMessage(Handler.java:95) JS: android.os.Looper.loop(Looper.java:146) JS: android.app.ActivityThread.main(ActivityThread.java:5602) JS: java.lang.reflect.Method.invokeNative(Native Method) JS: java.lang.reflect.Method.invoke(Method.java:515) JS: com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283) JS: com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099) JS: dalvik.system.NativeStart.main(Native Method)

I have created new issue on github repo based on my problem.

Why i cannot able to write the arraybuffer data into file using filesystem’s writeSync?
If i am doing wrong! then how can i write the array buffer into file in filesystem?

Cheers!


Suitable Http response type to handle bulk data response in single http call from Nativescript application
#2

I don’t think you should create three topics at the same time all for the same issue, it becomes like spamming


#3

@ganas Yes. I got your point.

But what i thought and created the three topics,

  1. First topic is for get confirmation about the Arraybuffer response support in nativescript
    Is NativeScript supports Arraybuffer/Bytearray HTTP response type

  2. Second topic is for if supports then how to consume Arraybuffer response in nativescript
    How consume the ArrayBuffer/ByteArray http response using nativescript filesystem

  3. Third topic is for get suggestion on which is suitable response type in order to handle bulk http responses on nativescript
    Suitable Http response type to handle bulk data response in single http call from Nativescript application