NativeScript / nativescript-background-http

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

Using uploadFile function in a service #84

Closed vipul1905 closed 6 years ago

vipul1905 commented 6 years ago

Hi,

I am trying to upload a single image file using serivces, but i get error on subscribe. The upload function does not return any values. Here is the code snippet. This below function is used from services.

this.userService.profileImageUpload(entity.path).subcribe((response)=>{
            console.dir(response);

       }, 
      (error) =>{      })

Below is the upload function in services

profileImageUpload(filePath){

    let token = appSettings.getString("token");
    if (token != ''){    
      var request = {  
        url: Config.apiUrl + "common/photo-preview-common",
        method: "POST",  
        headers: {
            "Content-Type": "application/octet-stream",
            "File-Name": "testfile",
     "Authorization": "Bearer " + token  
        },
        description: "{ 'uploading': 'testfile' }"
    };

    var params = [{name:"file", filename: filePath, mimeType: 'image/jpeg'}];
    //var task = session.multipartUpload(params, request);
    task = session.uploadFile(filePath, request);
    task.on('complete', logEvent)
    }   // end if token is not empty/ in case user logged out 

    function logEvent(e) {
      console.log(e.eventName)

  }

  }

Could anyone help me how to use it with service? Thank you.

tsonevn commented 6 years ago

Hi @vipul1905, I reviewed your case, however, there is no way to subscribe to the events provided by the nativescript-background-http plugin as it is shown in the attached code. In your case, you could use Promises in your customer service as it is shown in the attached sample project. Archive.zip

RoyiNamir commented 6 years ago

@tsonevn I'm facing a similar problem but in my case I think It doesn't find the file : https://github.com/NativeScript/nativescript-background-http/issues/88

care to help ?

tsonevn commented 6 years ago

Hi @vipul1905, Were you able to resolve your case or the issue still persists?