2016-08-09 32 views
0

Ich habe versucht, eine Lösung für meine Frage auf dieser Website zu finden, aber ich konnte mein Problem nicht lösen. es ist html Teil:

<form action="" method="post" enctype="multipart/form-data" id="edit_profile_form" > 
<input type="file" name="new_profile_photo" id="new_profile_photo" value="Choose Photo" > 
<button class="button11" name="save_changes" id="button11" >SAVE </button> 
</form> 

diese Ajax-Code ist:

<script type="text/javascript"> 
    $(document).on('click', '#button11', function(){ 
     event.preventDefault(); 

      var file_data = $("#new_profile_photo").prop('files')[0]; 
      var form_data = new FormData(); 
      form_data.append('file', file_data); 

     $.ajax({ 
        url:"ajax/editprofile.php", 
        method:"POST", 
        async: false, 
        cache: false, 
        contentType: false, 
        processData: false, 
        data: form_data, 

        success:function(data){ 
         $("#error_messages").html(data); 

        } 
       }); 


     }); 
</script> 

wenn ich var_dump ($ _ FILES) schreiben gibt es mir leeres Array. Wenn jemand weiß, dass er es lösen soll, bitte helfen. Dank

+0

Verwendung ajax laden 't Dateien hochladen. – phper

+0

Wenn ich form_data bekommen kann, werde ich Dateien hochladen. Ich denke, – user3557576

+0

Ajax Arbeit mit Flash (SWF) Datei kann Dateien hochladen. vielleicht jquery uplofify ist was du brauchst. – phper

Antwort

0

Try this:

JS

$('#upload').on('click', function() { 
      var file_data = $('#pic').prop('files')[0]; 
      var form_data = new FormData(); 
      form_data.append('file', file_data); 

      $.ajax({ 
        url   : 'upload.php',  // point to server-side PHP script 
        dataType : 'text',   // what to expect back from the PHP script, if anything 
        cache  : false, 
        contentType : false, 
        processData : false, 
        data  : form_data,       
        type  : 'post', 
        success  : function(output){ 
         alert(output);    // display response from the PHP script, if any 
        } 
      }); 
      $('#pic').val('');      /* Clear the file container */ 
     }); 

Php

<?php 
     if ($_FILES['file']['error'] > 0){ 
      echo 'Error: ' . $_FILES['file']['error'] . '<br>'; 
     } 
     else { 
      if(move_uploaded_file($_FILES['file']['tmp_name'], 'uploads/' . $_FILES['file']['name'])) 
      { 
       echo "File Uploaded Successfully"; 
      } 
     } 

    ?> 

Es wird die Datei auf Upload-Verzeichnis

+0

immer noch gleichen Fehler beheben: Undefinierter Index: Datei – user3557576

+0

Ich benutze den gleichen Code und es funktioniert gut. Überprüfen Sie, ob Ihre Datei-ID "Bild" ist oder nicht? –

+0

kannst du mir auf Facebook schreiben? https://www.facebook.com/binnet.musayev94 – user3557576