NativeScript / nativescript-background-http

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

Error on upload image, no response and respondeCode -1 #275

Closed PabloPG closed 3 years ago

PabloPG commented 3 years ago

If the demo apps cannot help and there is no issue for your problem, tell us about it

Same times when i try to upload a image, i receive this error:

Which platform(s) does your issue occur on?

Please, provide the following version numbers that your issue occurs with:


{
   "eventName":"error",
   "object":{
      "_observers":{
         "responded":[
            {

            }
         ],
         "error":[
            {

            }
         ]
      },
      "_session":{
         "_id":"image-upload"
      },
      "_id":"image-upload{12}",
      "_description":"Imagem-0",
      "_upload":680095,
      "_totalUpload":3601716,
      "_status":"error"
   },
   "error":{

   },
   "responseCode":-1,
   "response":null
}

I looked at several issues and didn't find a solution point for this type of case, does anyone know how to solve it?
syahnur197 commented 3 years ago

we also have similar issue, our codes are roughly like this, we are using Nativescript Vue

import { session } from "@nativescript/background-http";
const SESSION = session("image-upload");

class Something {

    constructor()
    {
        this.counter = 1;
        this.currentProgress = 0;
        this.maxProgress = 0;
        this.response = {};
    }

    post_bghttp(url, form_data) {
        let request = {
            url: GLOBAL.url() + url,
            method: "POST",
            headers: {
                Authorization: "Bearer " + AUTH.getToken(),
                // "Content-Type": "multipart/form-data"
                "Content-Type": "application/octet-stream"
            }
        };

        return await SESSION.multipartUpload(form_data, request);
    }

    bgHttpUpload(url, data) {
        try {
            let task = post_bghttp(url, data);

            task.on("progress", this.progressHandler);
            task.on("responded", this.respondedHandler);
            task.on("error", this.errorHandler);
            task.on("complete", this.completeHandler);
            task.on("cancelled", this.cancelledHandler);
        } catch (error) {
            // error occured here
            console.error(error);
        }
    }

    progressHandler(e) {
        this.currentProgress = e.currentBytes;
        this.maxProgress = e.totalBytes;
        if (this.counter === 1) {
            this.ntcPrint(
                "Total file size to be uploaded:",
                e.totalBytes / 1000000 + " MB"
            );
            this.counter++;
        } else {
            this.ntcPrint(e.currentBytes / 1000000, "MB uploaded.");
        }
    }

    errorHandler(e) {
            console.error("error" + JSON.stringify(e));
    }

    respondedHandler(e) {
        if (e.data.includes("success")) {
            this.response = JSON.parse(e.data);
        }
    }
}
syahnur197 commented 3 years ago

This is the data that we sent to the server for android phone

{
    "name": "file_upload",
    "filename": "/storage/emulated/0/WhatsApp/Media/WhatsApp Images/IMG-WA0003.jpg",
    "mimeType": "image/jpg"
}

is there something wrong the filename that we are using?

PabloPG commented 3 years ago

image More info:

Apparently it is sent 100%, but this error occurs after a while, but the image is on the server, only the return gave error.

Error is timeout, i can set a timeout config? image

PabloPG commented 3 years ago

Resolved.

After changing the timeout I managed to solve the problem

syahnur197 commented 3 years ago

Resolved.

After changing the timeout I managed to solve the problem

Hello there, may I know how do you change the timeout in nativescript?

PabloPG commented 3 years ago

Resolved. After changing the timeout I managed to solve the problem

Hello there, may I know how do you change the timeout in nativescript?

I changed only on android, adding:

method setRequestOptions on background-http.android.js (inside node_modules)

var timeout = options.timeout;
if (timeout) {
        net.gotev.uploadservice.UploadService.HTTP_STACK = new net.gotev.uploadservice.http.impl.HurlStack(true, false, timeout, timeout);
}

and on index.d.ts

/**
 * Use this to set the timeout connection in ms
 */
timeout?: number;