2016-08-11 3 views
1

Ich versuche, eine Datei (PDF) von AJAX auf meinen Server hochladen, um von einem PHP-Skript in einem Laravel-Projekt behandelt werden. Ich kann die Datei nicht zum Senden und Empfangen auf dem Server erhalten.Senden von PDF-Datei an Server (PHP LARAVEL) mit AJAX

Im Netzwerk kann ich sehen, dass die POST-Anfrage eine 200 Antwort erhält, jedoch gibt es eine Antwort von "Datei nicht vorhanden", die die Antwort von Laravel ist.

Auch in der Post-Anforderung die Anfrage-Payload enthält die folgenden

------WebKitFormBoundaryliAmA3wxs0bB32iZ-- 

Bitte beachten Sie die js und HTML und PHP siehe unten:

html

<form enctype="multipart/form-data"> 
    <input type="file" id="cv" name="cv"/> 
    <button id="file-send">Add</button> 
</form> 

js

$('#file-send').bind('click', function() { 
    $.ajax({ 
     url:"test", 
     data: new FormData($("#cv")[0]), 
     type:'POST', 
     processData: false, 
     contentType: false, 
     success:function(response){ 
     console.log(response); 
     }, 
    }); 
}); 

Laravel CODE

public static function uploadingFile(){ 
     if (Input::hasFile('cv')) 
{ 
    return "file present"; 
} 
else{ 
    return "file not present"; 
} 
+0

es nicht möglich, Datei durch Ajax-Request sendet, ist iframes möglich. – marko

+0

@marko bist du dir sicher? - http://stackoverflow.com/questions/5392344/sending-multipart-formdata-with-jquery-ajax –

Antwort

0

Versuchen Sie folgendes:

JS:

$('#file-send').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"; 
     } 
    } 

?> 
0

Dieses Plugin Versuchen Sie uploa d Datei

http://malsup.com/jquery/form/#file-upload

var options = { 
          beforeSubmit: showRequest, 
          success: showResponse, 
          dataType :'json'     
         }; 

    $('#file-send').ajaxSubmit(options); 

function showRequest() 
{ 
    //before uploading file 
} 

function showResponse() 
{ 
    //response after uploading file 
}