简体   繁体   中英

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

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:

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:

    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.

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

Any ideas why this might be happening? Thanks in advance!

try using this php code

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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