Ionic Upload File to Server


#1

This is my upload function

uploadresume()
    {
        this.fileChooser.open()
        .then(uri => 
        {
            console.log(uri)
            const fileTransfer: TransferObject = this.transfer.create();


            let options1: FileUploadOptions = {
                fileKey: 'file',
                fileName: 'name.pdf',
                params: {resume:uri},
                chunkedMode : false,
                headers: { Authorization:localStorage.getItem('token') }

            }
            console.log (localStorage.getItem('token'))

            fileTransfer.upload(uri, "http://website.com/upload", options1)
            .then((data) => {
                // success
                alert("success"+JSON.stringify(data));
            }, (err) => {
                // error
                alert("error"+JSON.stringify(err));
            });

        })
        .catch(e => console.log(e));
    }

now my API endPoint takes in the parameter Resume with the file attached to it. But I keep getting 500 error. I am sure it’s something to do with the way I am sending the file to the endpoint. Can someone please assist me ?


#2

your endpoint is a directory, it must be a script for example a PHP file that accept the file you have sent

try this one upload.php:

<?php

$target_path = "uploads/";
 
$target_path = $target_path . basename( $_FILES['file']['name']);
 
if(move_uploaded_file($_FILES['file']['tmp_name'], $target_path)) {
    echo "Upload and move success";
} else{
echo $target_path;
    echo "There was an error uploading the file, please try again!";
}
?>

the file transfer should be

fileTransfer.upload(uri, "http://website.com/upload.php", options1)

see to it that your php file has a write permission to the “upload” directory


#3

it still doesn’t work, i think because it’s sending a string of the URI and expecting a file in the server