0

I can't upload an image to external server from my app. When I try do this, VSCode return: ERROR Error: Cannot convert object to Ljava/lang/String; at index 0 . I use nativescript-background-http plugin in my app. My code:


    private file: string;
    private url: string;
    private counter: number = 0;
    private session: any;
    public events: { eventTitle: string, eventData: any }[] = [];
    constructor(private gallery: GalleryService, private params: ModalDialogParams) {
        this.image = params.context;
        var pict = new Image();
        pict.src = this.image;
        console.log(pict.src);
        this.file = pict.src;
        this.url = "http://127.0.0.1:80";
        this.session = bgHttp.session("image-upload");
    }
    uploadPhoto() {
        const name = this.file;
        const description = "Upload";
        const request = {
            url: this.url,
            method: "POST",
            headers: {
                "Content-Type": "application/octet-stream",
                "File-Name": name
            },
            description: description,
            androidAutoDeleteAfterUpload: false,
            androidNotificationTitle: 'NS HTTP back',
        };
        let task: bgHttp.Task;
        task = this.session.uploadFile(this.file, request);
    }
mark200
  • 47
  • 6
  • Did you test the API with any rest client? Did you test it on iOS? – Manoj Jan 10 '20 at 23:27
  • I believe you are brining image from image picker, which will give you an image asset. You will have to save the image to a local file and pass the path of the file. – Manoj Jan 10 '20 at 23:53
  • @Manoj I'm not test it on iOS. I tried save the image to a local file in service , but I don't know, why *selection* causes : Argument of type 'ImageAsset[]' is not assignable to parameter of type 'ImageAsset'. My code :https://pastebin.com/bsccYYTi – mark200 Jan 11 '20 at 13:12
  • 1
    I guess the error is clear enough, the picker will return array of images, and you need the first image from that. Access the image asset at index 0, do not pass it as it is. – Manoj Jan 11 '20 at 14:42

0 Answers0