Image使用cordova文件插件上传到数据库

时间:2018-04-16 08:32:28

标签: php android cordova phonegap

我在使用cordova file-transferplugin将图像上传到数据库时遇到问题,我已成功上传到服务器但我也想将图像上传到服务器,因为该图像将作为用户的个人资料图片应用程序我不知道我将如何去做thiis是我的PHP代码

function getImage() {
    navigator.camera.getPicture(uploadPhoto, function (message) {
        var toast = app.toast.create({
            text: 'Error getting picture',
            position: "bottom"
        })
        toast.open();
    }, {
        quality: 100,
        destinationType: navigator.camera.DestinationType.FILE_URI,
        sourceType: navigator.camera.PictureSourceType.PHOTOLIBRARY
    });

}


var imageData,error,result
function uploadPhoto(imageURI) {
    
    imageData = imageURI
    alert(imageData)
    var options = new FileUploadOptions();
    options.fileKey = "file";
    options.fileName = imageURI.substr(imageURI.lastIndexOf('/') + 1);
    options.mimeType = "image/jpeg";
    console.log(options.fileName);
    var params = new Object();
    params.value1 = "test";
    params.value2 = "param";

    options.params = params;
    options.chunkedMode = false;

    var ft = new FileTransfer();
    // ft.upload(imageURI, "http://127.0.0.1gimbayapi/image_upload.php", function (result) {
    //     alert(JSON.stringify(result));  
    // ft.upload(imageURI, "https://0a3efd2c.ngrok.io/gimbayapi/image_upload.php?username=" + user_name + "&user_id="+user_id, function (result) {
    //     alert(JSON.stringify(result));  
    
    ft.upload(imageURI, "https://gimbayapi.000webhostapp.com/gimbayapi/image_upload.php?username=" + user_name + "&user_id="+user_id, function (result) {
        alert(JSON.stringify(result));  
    
    }, function (error) {
        alert(JSON.stringify(error));
    }, options);
}
<?php
//Allow Headers
header('Access-Control-Allow-Origin: *');
require 'config.php';

//print_r(json_encode($_FILES));
$new_image_name = urldecode($_FILES["file"]["name"]).".jpg";

$username = $_REQUEST['username'];
$user_id = $_REQUEST['user_id'];

$sql = "UPDATE user set image = '$new_image_name' WHERE id='$user_id' AND username ='$username' ";

$result = $connection->query($sql);
if ($result === true) {
    //Move your files into upload folder
    move_uploaded_file($_FILES["file"]["tmp_name"], "uploads/" . $new_image_name);
    $message = [
        'messagetype' => 'success',
        'message' => 'successfully'
    ];
    echo json_encode($message);
} 

?>

我使用framework7作为我的Cordova框架

0 个答案:

没有答案