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

Tashen Jazbi
  • 1,068
  • 1
  • 16
  • 41
  • I think when you append a file to a `FormData` object you have to append it last. Maybe try appending you `id` first? – arbuthnott Aug 17 '17 at 11:41
  • @arbuthnott i tried this, but no luck :( – Tashen Jazbi Aug 17 '17 at 11:45
  • the fact that it is an audio file or not shouldn't matter for the upload part, – DarkMukke Aug 17 '17 at 12:02
  • 5
    Possible duplicate of [How to send FormData objects with Ajax-requests in jQuery?](https://stackoverflow.com/questions/6974684/how-to-send-formdata-objects-with-ajax-requests-in-jquery) or https://stackoverflow.com/questions/5392344/sending-multipart-formdata-with-jquery-ajax – DarkMukke Aug 17 '17 at 12:02

0 Answers0