NativeScript / nativescript-background-http

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

Error: Cannot convert object to I at index 1 #119

Closed bhavincb closed 6 years ago

bhavincb commented 6 years ago
let session=bghttp.session("image-upload");
        let fileUri = path;
        let filename = fileUri.substring(fileUri.lastIndexOf('/')+1);
        let mimetype = filename.substring(filename.lastIndexOf('.')+1);
        let uploadType = "image";
        let request:bghttp.Request = {
            url: url,
            method: 'POST',
            headers: {
                "Content-Type": "application/octet-stream",
                "File-Name": filename,
                'Authorization': 'JWT '+this.user.token,
            },
            description: 'Uploading '+filename,
            androidDisplayNotificationProgress:true
        };
        let task = session.multipartUpload(data, request);
        task.on('progress',(e)=>{
            this.uploadPercentage=this.fileTransferOnProgress(e,this.uploadNotificationId,this.uploadPercentage);
        });
        task.on('error', (e) => {
            console.log("file Upload plugin Error",e);
            reject(e);
        });
        // task.on('complete', (e) => resolve(e));
        task.on("responded", (e)=>{
            resolve(JSON.parse(e.data));
        });

when i'm using above code for file upload with nativescript-telegram-image-picker plugin. and path is set to what i'm getting from plugin

openTelegramImagePicker(1).then((resp: TelegramPickerResponse) => {

          let fileUrl=resp.photos[0];
          // let src=new ImageSource();
          // src.fromFile(fileUrl).then(source=>{
          //   let image:any={};
          //   image.fileURL=fileUrl;
          //   image.imageSource=source;
          //   image.toURL=function(){
          //     return this.fileURL;
          //   }
          //   this.data.selectedGalleryImages.unshift(image);

          // });
          let image:any={};
          image.fileURL=fileUrl;
          image.toURL=function(){
            return this.fileURL;
          }
          // this.data.selectedGalleryImages.unshift(image);
          images.unshift(image);
          // resolve(image);

        resolve(image);
      }).catch((error)=>{
        reject(error);
      })

i'm getting this error Error: Cannot convert object to I at index 1

i also tried using nativescript camera plugin also

requestPermissions().then(
        ()=>{

          takePicture(cameraOptions).then((imageAsset)=>{

            /// working only for android
            if(isAndroid){
              // let src=new ImageSource();
              // src.fromAsset(imageAsset).then(source=>{
              //   let image:any={};
              //   image.fileURL=imageAsset.android;
              //   image.imageSource=source;
              //   image.toURL=function(){
              //     return this.fileURL;
              //   }
              //   this.data.selectedCameraImages.unshift(image);
              //   resolve(image);
              // });
              let image:any={};
              image.fileURL=imageAsset.android;
              image.toURL=function(){
                return this.fileURL;
              }
              this.data.selectedCameraImages.unshift(image);
              resolve(image);
            }
          }).catch((error)=>{
            console.log("error taking picture");
            reject(error);
          })
        },
        ()=>{
          alert("Please Provide Permision for camera and try again");
          reject(false);
        }
      )

but still getting the same error.

bhavincb commented 6 years ago

it was my mistake , by mistake i had set non string value to parameters which was causing this issue. i had solved it by doing below

let data = [
        { name: filename, filename: fileUri.toString(), mimeType: 'image/'+mimetype },
        { name:'userId', value:this.user.user.id.toString()},
        { name:'grpId', value:grp.id.toString()}
    ];