NativeScript / nativescript-background-http

Background Upload plugin for the NativeScript framework
Apache License 2.0
102 stars 50 forks source link

Multipart upload doesn't work on Android #72

Closed bachras closed 7 years ago

bachras commented 7 years ago

Hi all,

I have tried this plugin with sample app from https://github.com/NativeScript/sample-ImageUpload and it works perfectly fine, but when I tried to change it to multipart upload I get this error on Android (iOS works fine).

In example I just changed this line:

var task = session.uploadFile(fileUri, request);

to this:

var params = [{name: "firstName", value: "Dave"}, {name:"file", filename: fileUri, mimeType: 'image/jpeg'}];
var task = session.multipartUpload(params, request);  

Error:

JS: ERROR Error: Uncaught (in promise): Error: java.lang.Exception: Failed resolving method addParameter on class net.gotev.uploadservice.MultipartUploadRequest
JS:     com.tns.Runtime.resolveMethodOverload(Runtime.java:982)
JS:     com.tns.Runtime.callJSMethodNative(Native Method)
JS:     com.tns.Runtime.dispatchCallJSMethodNative(Runtime.java:1021)
JS:     com.tns.Runtime.callJSMethodImpl(Runtime.java:903)
JS:     com.tns.Runtime.callJSMethod(Runtime.java:890)
JS:     com.tns.Runtime.callJSMethod(Runtime.java:874)
JS:     com.tns.Runtime.callJSMethod(Runtime.java:866)
JS:     com.tns.NativeScriptActivity.onActivityResult(NativeScriptActivity.java:69)
JS:     android.app.Activity.dispatchActivityResult(Activity.java:6192)
JS:     android.app.ActivityThread.deliverResults(ActivityThread.java:3570)
JS:     android.app.ActivityThread.handleSendResult(ActivityThread.java:3617)
JS:     android.app.ActivityThread.access$1300(ActivityThread.java:151)
JS:     android.app.ActivityThread$H.handleMessage(ActivityThread.java:1352)
JS:     android.os.Handler.dispatchMessage(Handler.java:102)
JS:     android.os.Looper.loop(Looper.java:135)
JS:     android.app.ActivityThread.main(ActivityThread.java:5254)
JS:     java.lang.reflect.Method.invoke(Native Method)
JS:     java.lang.reflect.Method.invoke(Method.java:372)
JS:     com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
JS:     com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
JS: Error: java.lang.Exception: Failed resolving method addParameter on class net.gotev.uploadservice.MultipartUploadRequest
JS:     com.tns.Runtime.resolveMethodOverload(Runtime.java:982)
JS:     com.tns.Runtime.callJSMethodNative(Native Method)
JS:     com.tns.Runtime.dispatchCallJSMethodNative(Runtime.java:1021)
JS:     com.tns.Runtime.callJSMethodImpl(Runtime.java:903)
JS:     com.tns.Runtime.callJSMethod(Runtime.java:890)
JS:     com.tns.Runtime.callJSMethod(Runtime.java:874)
JS:     com.tns.Runtime.callJSMethod(Runtime.java:866)
JS:     com.tns.NativeScriptActivity.onActivityResult(NativeScriptActivity.java:69)
JS:     android.app.Activity.dispatchActivityResult(Activity.java:6192)
JS:     android.app.ActivityThread.deliverResults(ActivityThread.java:3570)
JS:     android.app.ActivityThread.handleSendResult(ActivityThread.java:3617)
JS:     android.app.ActivityThread.access$1300(ActivityThread.java:151)
JS:     android.app.ActivityThread$H.handleMessage(ActivityThread.java:1352)
JS:     android.os.Handler.dispatchMessage(Handler.java:102)
JS:     android.os.Looper.loop(Looper.java:135)
JS:     android.app.ActivityThread.main(ActivityThread.java:5254)
JS:     java.lang.reflect.Method.invoke(Native Method)
JS:     java.lang.reflect.Method.invoke(Method.java:372)
JS:     com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
JS:     com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
JS:     at Function.Task.createMultiPart (file:///data/data/org.xxxxxx.xxxxxx/files/app/tns_modules/nativescript-background-http/background-http.js:132:25)
JS:     at Session.multipartUpload (file:///data/data/org.xxxxxx.xxxxxx/files/app/tns_modules/nativescript-background-http/background-http.js:70:21)
JS:     at sendImages (file:///data/data/org.xxxxxx.xxxxxx/files/app/pages/mainTabs/mainTabs.component.js:1385:36)
JS:     at file:///data/data/org.xxxxxx.xxxxxx/files/app/pages/mainTabs/mainTabs.component.js:1350:40
JS:     at ZoneDelegate.invoke (file:///data/data/org.xxxxxx.xxxxxx/files/app/tns_modules/nativescript-angular/zone-js/dist/zone-nativescript.js:365:26)
JS:     at Object.onInvoke (file:///data/data/org.xxxxxx.xxxxxx/files/app/tns_modules/@angular/core/bundles/core.umd.js:4156:37)
JS:     at ZoneDelegate.invoke (file:///data/data/org.xxxxxx.xxxxxx/files/app/tns_modules/nativescript-angular/zone-js/dist/zone-nativescript.js:364:32)
JS:     at Zone.run (file:///data/data/org.xxxxxx.xxxxxx/files/app/tns_modules/nativescript-angular/zone-js/dist/zone-nativescript.js:125:43)
JS:     at file:///data/data/org.xxxxxx.xxxxxx/files/app/tns_modules/nativescript-angular/zone-js/dist/zone-nativescript.js:760:57
JS:     at ZoneDelegate.invokeTask (file:///data/data/
JS: org.xxxxxx.xxxxxx/files/app/tns_modules/nativescript-angular/zone-js/dist/zone-nativescript.js:398:31)
JS:     at Object.onInvokeTask (file:///data/data/org.xxxxxx.xxxxxx/files/app/tns_modules/@angular/core/bundles/core.umd.js:4147:37)
JS:     at ZoneDelegate.invokeTask (file:///data/data/org.xxxxxx.xxxxxx/files/app/tns_modules/nativescript-angular/zone-js/dist/zone-nativescript.js:397:36)
JS:     at Zone.runTask (file:///data/data/org.xxxxxx.xxxxxx/files/app/tns_modules/nativescript-angular/zone-js/dist/zone-nativescript.js:165:47)
JS:     at drainMicroTaskQueue (file:///data/data/org.xxxxxx.xxxxxx/files/app/tns_modules/nativescript-angular/zone-js/dist/zone-nativescript.js:593:35)

Any help would be appreciated. thanks

NickIliev commented 7 years ago

Hi, @bachras I have tested the sample application (on LG Nexus 5x) with your change for the multi-part upload and it works on my side as expected. Can you clarify on what API level you are testing?

bachras commented 7 years ago

Hi @NickIliev, thank you for reply. I have just tried on Nexus 5 (API 22) and it seems that error doesn't appear on the test app, but nothing is send to server side. Task events are not logging.

I am developing NS Angular app and I still get the same error as I mentioned earlier. Example is based on NS Vanilla JS and my app is Angular. Could that be the reason. If so how could I easily convert to NS Angular. Thanks

jeffswitzer commented 7 years ago

@bachras I'm not using the sample app, but I tried your method in a nativescript/angular app I've been building to upload a sqlite database to a node server using multer middleware and it seems to work. I've only tested on android so far (droid 2 turbo).

jeffswitzer commented 7 years ago

@bachras one more thing, I did get a similar error to the one you posted after experimenting more with this. My issue was that I had let params = [{name: "person_id", value: this._peopleService.selectedPerson.person_id}, {name:"file", filename: dbname, mimeType: 'application/x-sqlite3'}]; where this._peopleService.selectedPerson.person_id was a number so I just did this and the error went away value: this._peopleService.selectedPerson.person_id.toString()

AceTheFace commented 7 years ago

Thanks @jeffswitzer. toString() fixed the problem for me!

radeva commented 7 years ago

A note is added in the README to explicitly specify that the values sent to multipartUpload method should be strings.