donk68254 2018-04-16 08:32
浏览 77

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

i have a problem uploading images to a databse using cordova file-transferplugin, i have successfully uploaded to a serve but i also wnat the image to be uploaded to a serve because the image will serve as a profile picture for users using the app i dont know how i will go about it thiis is my php code

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);
} 

?>

I am using framework7 as my Cordova framework

</div>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 #MATLAB仿真#车辆换道路径规划
    • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
    • ¥15 数据可视化Python
    • ¥15 要给毕业设计添加扫码登录的功能!!有偿
    • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
    • ¥15 微信公众号自制会员卡没有收款渠道啊
    • ¥15 stable diffusion
    • ¥100 Jenkins自动化部署—悬赏100元
    • ¥15 关于#python#的问题:求帮写python代码
    • ¥20 MATLAB画图图形出现上下震荡的线条