Cordova fileupload无法在Android上运行,$ _FILES为空

时间:2016-02-16 09:03:41

标签: php android cordova cordova-plugins file-transfer

我正在使用cordova filetransfer插件将一些图片上传到服务器。它在我的localhost(Android和iOS)上运行良好,但在我的服务器上它只适用于iOS。使用Android,$ _FILES数组为空......

filetransfer插件会触发uploadSuccess回调,但我无法获得图片服务器端...

服务器端:

public function upload_img_api(){
    file_put_contents('test.txt', print_r($_FILES, true)); //<-test.txt returns empty array :(
    $path_parts = pathinfo($_FILES["file"]["tmp_name"]);
    $extension = strtolower(substr($path_parts['extension'], 0, strpos($path_parts['extension'], "?")));
    $extension = ($extension) ? ".".$extension : ".jpg";
    $image_size = getimagesize($_FILES["file"]["tmp_name"]);
    $image_w = $image_size[0];
    $image_h = $image_size[1];
    $megapixel = $image_w * $image_h;
    $ratio = $this->getResizeRatio($megapixel);
    $image_name = "api_image_".time().$extension;
    if (!move_uploaded_file($_FILES["file"]["tmp_name"], DENT_UPLOAD_TEMP.$image_name)){
        die(json_encode(array('Result' => "ERROR", "MESSAGE" => 'COULDNT FIND FILE')));
    }
    $thumb_path = $this->generate_thumbnail(DENT_UPLOAD_TEMP.$image_name, DENT_UPLOAD_TEMP.'thumb_'.$image_name);
    $resize_image_path = $this->generate_thumbnail(DENT_UPLOAD_TEMP.$image_name, DENT_UPLOAD_TEMP.$image_name, array($image_w/$ratio, $image_h/$ratio));
    die(json_encode(array('file_name' => $image_name)));
}

有什么建议吗?

谢谢! :)

1 个答案:

答案 0 :(得分:3)

如果我将cordova fileupload插件选项设置为: chunkedMode = false 它可以工作......我也改变了te fileKey ,现在你必须检查 $ _ FILES [&#34; fileUpload&#34;] [&#34; tmp_name&#34;] 在服务器端。

以下是工作选项:

options.fileKey="fileUpload";
options.fileName=fileURI.substr(fileURI.lastIndexOf('/')+1);
options.mimeType="image/jpeg";
options.httpMethod="POST";
options.chunkedMode=false;

希望它能帮助别人:)