【问题标题】:Cordova Camera Image Not Uploading Using File TransferCordova 相机图像未使用文件传输上传
【发布时间】:2018-03-09 22:44:16
【问题描述】:

我目前所做的想法是,您可以拍照或从图库中选择一张并将其上传到服务器。我在使用科尔多瓦FileTransfer 发送和上传图片时遇到了麻烦。要么根本没有发送,要么$_FILES["file"] 为空。

我有单独的按钮来调出相机和画廊:

<button id="takePicture" name="takePicture" ng-click="openCamera();">Take Photo</button>
<button id="getPicture" name="getPicture" ng-click="openGallery();">Choose From Gallery</button>

解决方案:

$scope.openCamera = function()
{
    navigator.camera.getPicture(onSuccess, onError, 
    {   quality         : 100, 
        destinationType : Camera.DestinationType.FILE_URI
    });  

    function onSuccess(imageURI) 
    { 
        var options = new FileUploadOptions();

        options.fileKey     = "file";
        options.fileName    = imageURI.substr(imageURI.lastIndexOf("/")+1);
        options.mimeType    = "image/jpeg";
        options.httpMethod  = "POST";
        options.chunkedMode = false;
        options.params      = { filePath : imageURI.split("?")[0] };

        var fileTransfer = new FileTransfer;

        fileTransfer.upload(imageURI, encodeURI("upload.php"), uploadComplete, uploadError, options);

        function uploadComplete(result) 
        {
            console.log("Code = " + result.responseCode);
            console.log("Response = " + result.response);
            console.log("Sent = " + result.bytesSent);
        }

        function uploadError(error) 
        {
            alert("An error has occurred: Code = " + error.code);
            console.log("upload error source " + error.source);
            console.log("upload error target " + error.target);
        }
    }  

    function onError(message) 
    { 
        alert("fail");

        alert('Failed because: ' + message); 
    } 
}

然后将在upload.php 文件中接收发送的数据。您应该能够通过检查文件var_dump($_FILES);来检查数据是否已发送

正如Sletheren 提到的,也可以使用$cordovaFileTransfer.upload(); 来完成

【问题讨论】:

    标签: angularjs cordova file-transfer


    【解决方案1】:

    这就是我这边的工作方式:

    • 第一件事:destinationType 应该是Camera.DestinationType.FILE_URI
    • 第二:设置文件名为:filename = imageURI.split('?')[0];
    • 第三:fileTransfer 采用这些参数(它适用于我): $cordovaFileTransfer.upload(url, filename, options)

    【讨论】:

    • 我现在就试一试。谢谢! $cordovaFileTransfer();ft.upload(); 的工作方式相同,不是吗?
    • 我不确定,自从我上次使用 Ionic 1.x 已经有一段时间了,如果在 ft.upload 上不起作用,那么你可以回滚到 $cordovaFileTransfer
    • 我正在尝试打印出$_FILES["file"]["tmp_name"],但我目前收到的是Undefined index: file
    • filename首先发送到服务器之前尝试检查它的值
    • 文件名已填充。似乎imageURI.split('?')[0]; 检索文件路径,imageURI.substr(imageURI.lastIndexOf("/")+1); 检索文件名,但我不太清楚为什么$_FILES["file"] 仍然为空。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-08-16
    • 1970-01-01
    • 2016-12-18
    • 2018-02-14
    • 2016-05-14
    • 2015-09-24
    • 1970-01-01
    相关资源
    最近更新 更多