K文件提交服务器计算,在phonegap中上传0K文件?(uploading 0K file in phonegap?)

Now I am using the file deliver plugin in phonegap to upload a wav file recorded on ipad. However, when I ran the code of uploading, it just uploaded a 0k file to the server.

I am sure I have followed the guide in the API document of phonegap, and set chunkmode=false. I also try to revise the path parameter to 'tmp' as the following document mentioned, but it doesn't work. PhoneGap .wav upload from an iOS device is creating a 0k file on the server

What's wrong with it? I totally get confused. Could anyone help?

This is the code in ios device.

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);

}

This is the code on server

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
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值