使用file-transfer cordova插件将android上传到服务器(Codeigniter)

时间:2016-03-02 19:32:53

标签: angularjs ajax codeigniter ionic-framework http-post

我是离子新手,我很难创建一个可以从photolibrary甚至相机上传图像的功能。我使用phonegap测试它。我正在使用离子框架并使用Cordova文件传输。打开photolibrary和相机的功能很好,但我不知道如何使用http POST将其保存到我的服务器(CI)中。请帮助..

这是我的控制器:

  $scope.selectPicture = function() { 
    var options = {
        quality: 50,
        destinationType: Camera.DestinationType.FILE_URI,
        sourceType: Camera.PictureSourceType.PHOTOLIBRARY
    };

  $cordovaCamera.getPicture(options).then(
    function(imageURI) {
        window.resolveLocalFileSystemURI(imageURI, function(fileEntry) {
            $scope.picData = fileEntry.nativeURL;
            $scope.ftLoad = true;
            var image = document.getElementById('myImage');
            image.src = fileEntry.nativeURL;
        });
        $ionicLoading.show({template: 'Foto acquisita...', duration:500});
    },
    function(err){
        $ionicLoading.show({template: 'Errore di caricamento...', duration:500});
    })
};

$scope.uploadPicture = function() {
    $ionicLoading.show({template: 'Sto inviando la foto...'});
    var fileURL = $scope.picData;
    var options = new FileUploadOptions();
    options.fileKey = "file";
    options.fileName = fileURL.substr(fileURL.lastIndexOf('/') + 1);
    options.mimeType = "image/jpeg";
    options.chunkedMode = true;

    var params = {};
    params.value1 = "someparams";
    params.value2 = "otherparams";

    options.params = params;

    var ft = new FileTransfer();
    ft.upload(fileURL, encodeURI("http://www.yourdomain.com/upload.php"), viewUploadedPictures, function(error) {$ionicLoading.show({template: 'Errore di connessione...'});
    $ionicLoading.hide();}, options);
}

var viewUploadedPictures = function() {
    $ionicLoading.show({template: 'Sto cercando le tue foto...'});
    server = "http://www.yourdomain.com/upload.php";
    if (server) {
        var xmlhttp = new XMLHttpRequest();
        xmlhttp.onreadystatechange=function(){
        if(xmlhttp.readyState === 4){
                if (xmlhttp.status === 200) {                    
            document.getElementById('server_images').innerHTML = xmlhttp.responseText;
                }
                else { $ionicLoading.show({template: 'Errore durante il caricamento...', duration: 1000});
                return false;
                }
            }
        };
        xmlhttp.open("GET", server , true);
        xmlhttp.send()} ;
    $ionicLoading.hide();
}`

我只是在谷歌看到这个代码,我试试。幸运的是,打开库和相机工作的功能。唯一的问题是我不知道如何将图像保存到服务器(CI)..

0 个答案:

没有答案