0

I've written code to submit audio file to server but i'm not getting success response from ajax. here is the html

<input type='file' name='music' id='default_files'>                        
<div id='refpic' value='Save'style=' border : 1px solid #73963e;' >Save</div>

here is the ajax call code on button click

<script type="text/javascript">

$('#refpic').click(function(){

    var formdata = new FormData();
    var file = $('#default_files').prop('files')[0];
    formdata.append("refaudio", file);
    formdata.append("id", '1');

    $.ajax
    ({ 
        url: 'change_image_audio.php',
        data: formdata,
        type: 'post',
        success: function(result)
        {
            alert('ok');
        }
    });
});


</script>

here is my php code

<?php

$id=$_POST['id'];


$target_path = "sounds/";

$target_path = $target_path . basename($_FILES['refaudio']['name']);

if(move_uploaded_file($_FILES['refaudio']['tmp_name'], $target_path)){

    echo "success";

}


?>

Please help me to find where i'm doing wrong. Any help would be much appreciated. Thank you

4
  • I think when you append a file to a FormData object you have to append it last. Maybe try appending you id first? Commented Aug 17, 2017 at 11:41
  • @arbuthnott i tried this, but no luck :( Commented Aug 17, 2017 at 11:45
  • the fact that it is an audio file or not shouldn't matter for the upload part, Commented Aug 17, 2017 at 12:02
  • 5
    Possible duplicate of How to send FormData objects with Ajax-requests in jQuery? or stackoverflow.com/questions/5392344/… Commented Aug 17, 2017 at 12:02

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.