简体   繁体   English

上载到服务器时,PhoneGap文件传输损坏.mp3

[英]PhoneGap File Transfer corrupts .mp3 when uploading to server

I'm messing around with PhoneGap for a project, but i'm having some trouble uploading an .mp3 我正在为某个项目使用PhoneGap,但是在上载.mp3时遇到了一些麻烦

I'm able to record the mp3 using the device's in-built microphone, and I'm then able to upload the file using the following JavaScript: 我可以使用设备的内置麦克风录制mp3,然后可以使用以下JavaScript上传文件:

var audio_name = 'fyp_app_recording_' + Math.round(new Date().getTime()/1000) + '.mp3';

// Record audio
// 
function recordAudio() {

    var src = audio_name;
    var mediaRec = new Media(src, onSuccess(src), onError);

    // Record audio
    mediaRec.startRecord();

    // Stop recording after 10 sec
    var recTime = 0;
    var recInterval = setInterval(function() {
        recTime = recTime + 1;
        setAudioPosition("Recording Audio - " + recTime + " sec");
        if (recTime >= 10) {
            clearInterval(recInterval);
            mediaRec.stopRecord();
            setAudioPosition("Recording Saved As " + src);
        }
    }, 1000);
}

// onSuccess Callback
//
function onSuccess(src) {
    console.log("recordAudio():Audio Success" + src);
}

// onError Callback 
//
function onError(error) {
    alert('code: '    + error.code    + '\n' + 
          'message: ' + error.message + '\n');
}

// Set audio position
// 
function setAudioPosition(position) {
    document.getElementById('audio_position').innerHTML = position;
}

//File upload

    function uploadAudio() {    

      var options = new FileUploadOptions();
        options.fileKey="file";
        options.fileName="test.mp3";
        options.mimeType="audio/mpeg";

        var ft = new FileTransfer();
        ft.upload("mnt/sdcard/" + audio_name, "http://www.richardsonweb.co.uk/fyp/test1/upload.php", win, fail, options);
    }

    function win() {
        alert("Yay! It worked!");
    }

    function 7(error) {
        alert("An error has occurred: Code = " = error.code);
    }

The upload.php file contains the following code: upload.php文件包含以下代码:

    print_r($_FILES);



$new_file_name = time() . ".mp3";
move_uploaded_file($_FILES["file"]["tmp_name"], "upload/".$new_file_name);

However, when I then download the file from my FTP client, Windows Media Player claims the file is corrupted. 但是,当我从FTP客户端下载文件时,Windows Media Player声称文件已损坏。

Oddly, the file sort of plays fine in VLC (the progress bar jams at 10 seconds, and doesn't move, but it plays the audio perfectly). 奇怪的是,该文件在VLC中可以正常播放(进度条在10秒钟处卡住,并且不会移动,但可以完美播放音频)。

Any ideas why this might be happening? 任何想法为什么会发生这种情况? Thanks in advance! 提前致谢!

try using this php code 尝试使用此php代码

<?php
   $destination_path = getcwd().DIRECTORY_SEPARATOR;

   $result = 0;

   $target_path = $destination_path . time() . ".mp3";

   ini_set("upload_max_filesize", "3200000000");

   if(@move_uploaded_file($_FILES['file']['tmp_name'], $target_path)) {
      $result = 1;
   }

   sleep(1);
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM