使用AJAX将PDF文件发送到服务器(PHP LARAVEL)

我试图通过AJAX将文件(PDF)上传到我的服务器,由laravel项目中的php脚本处理。 我无法在服务器上获取要发送和接收的文件。

在网络中,我可以看到POST请求获得了200响应,但是它返回了‘file not present’的响应,这是来自laravel的响应。

同样在post请求中,Request Payload包含以下内容

------WebKitFormBoundaryliAmA3wxs0bB32iZ-- 

请参阅下面的js和html和php:

HTML

 

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"; } 

试试这个:

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的:

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

尝试使用此插件上传文件

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 }