在phonegap中上传0K文件?

时间:2013-11-15 16:21:26

标签: cordova upload

现在我正在使用phonegap中的文件传递插件来上传在ipad上录制的wav文件。但是,当我运行上传代码时,它只是将0k文件上传到服务器。

我确信我已经遵循了phonegap的API文档中的指南,并设置了chunkmode = false。我还尝试将path参数修改为'tmp',如下面提到的文档,但它不起作用。 PhoneGap .wav upload from an iOS device is creating a 0k file on the server

它出了什么问题?我完全糊涂了。有人可以帮忙吗?

这是ios设备中的代码。

function upload(){

//src="test.txt";
console.log("get into send.");
var win = function(r) {
    console.log("Code = " + r.responseCode);
    console.log("Response = " + r.response);
    console.log("Sent = " + r.bytesSent);
}

var fail = function(error) {
    console.log ("An error has occurred: Code = " + error.code);
}

var currentTimeStamp=new Date(Date.now());
var testDate=currentTimeStamp.getFullYear() + "-" +
    currentTimeStamp.getMonth() + "-" +
    currentTimeStamp.getDate() + " " +
    currentTimeStamp.getHours() + ":" +
    currentTimeStamp.getMinutes() + ":" +
    currentTimeStamp.getSeconds();

var options = new FileUploadOptions();
options.fileKey="file";
options.fileName=src;
options.fileName=userId + Date.now()+".wav";
options.mimeType="audio/wav";
options.chunkedMode = false;

var params = new Object();
params.userId = userId;
params.startLevel = startLevel;
params.passageId=passageId;
params.passageContent=passageContent;
params.fileName=options.fileName;
params.testDate=testDate;

options.params = params;

console.log("begin send.");

var ft = new FileTransfer();
console.log("srcFullPath:" + srcFullPath);
ft.upload(srcFullPath, "http://insys.vmhost.psu.edu/~jxl1089/RTR/server/upload.php", win, fail, options);

}

这是服务器上的代码

<?php

require("inc/db.php");
$upload_key = 'file';

if (isset($_FILES[$upload_key])) {

  try {

    $error = $_FILES[$upload_key]['error'];
    if (is_array($error))
        throw new Exception('This script can\'t accept multiple files');
    switch ($error) {
        case UPLOAD_ERR_INI_SIZE:
            throw new Exception('Exceeded upload_max_filesize');
        case UPLOAD_ERR_FORM_SIZE:
            throw new Exception('Exceeded MAX_FILE_SIZE');
        case UPLOAD_ERR_PARTIAL:
            throw new Exception('Incomplete file uploaded');
        case UPLOAD_ERR_NO_FILE:
            throw new Exception('No file uploaded');
        case UPLOAD_ERR_NO_TMP_DIR:
            throw new Exception('No tmp directory');
        case UPLOAD_ERR_CANT_WRITE:
            throw new Exception('Can\'t write data');
        case UPLOAD_ERR_EXTENSION:
            throw new Exception('Extension error');
    }

    $finfo    = new finfo(FILEINFO_MIME);
    $name     = $_FILES[$upload_key]['name'];
    $tmp_name = $_FILES[$upload_key]['tmp_name'];
    $size     = $_FILES[$upload_key]['size'];


    if ($size > 1000000)
        throw new Exception('Exceeded 1MB limit');
    if (!is_uploaded_file($tmp_name))
        throw new Exception('Not an uploaded file');

    $type = $finfo->file($tmp_name);

    if ($type === false)
        throw new Exception('Failed to get MimeType');
    //if (substr($type, 'image/') !== 0);
    //    throw new Exception('Only images available');

    $new_name = dirname(__FILE__).'/audio/'.$name;

    if (is_file($new_name))
        throw new Exception("The file {$new_name} already exists");

    if (!move_uploaded_file($tmp_name, $new_name))
        throw new Exception('Failed to move uploaded file');

    $msg = "File successfully uploaded as {$new_name}.
            filesize:{$size}.
            userId:{$_POST[userId]}.
            startLevel:{$_POST[startLevel]}.
            fileName:{$_POST[fileName]}.
            testDate:{$_POST[testDate]}.
            passageId:{$_POST[passageId]}.
            passageContent:{$_POST[passageContent]}.
            "; 

} catch (Exception $e) {

    $msg = 'Error: '.$e->getMessage();

}

} else {

$msg = 'No file sent';

}

echo $msg;
?>

1 个答案:

答案 0 :(得分:0)

问题解决了。

问题是我没有将媒体源的整个路径作为第一个参数放入getMedia方法。

无论如何,我希望科尔多瓦能够在其文件中给予更多解释。