2013-11-15 34 views
0

現在我使用phonegap中的文件傳送插件來上傳記錄在ipad上的wav文件。但是,當我運行上傳代碼時,它只是上傳一個0k文件到服務器。在手機上傳0K文件?

我確定我已經按照phonegap的API文檔中的指南設置了chunkmode = false。我還嘗試將路徑參數修改爲'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; 
?> 

回答

0

問題解決了。

問題是我沒有把媒體源的整個路徑作爲第一個參數放入getMedia方法中。

無論如何,我希望科爾多瓦能在文件中給出更多的解釋。

+0

你能發送你找到的修復程序的完整代碼嗎? –