gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 05:41:24 24 4
gpt4 key购买 nike

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

在网络中,我可以看到 POST 请求收到 200 响应,但它返回“文件不存在”响应,这是来自 Laravel 的响应。

此外,在发布请求中,请求负载包含以下内容

------WebKitFormBoundaryliAmA3wxs0bB32iZ--

请参见下面的js、html和php:

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 代码

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:

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

?>

关于javascript - 使用 AJAX 将 PDF 文件发送到服务器 (PHP LARAVEL),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38893450/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com