【发布时间】:2016-12-18 00:30:03
【问题描述】:
我正在尝试通过 AJAX 将文件 (PDF) 上传到我的服务器,以由 laravel 项目中的 php 脚本处理。我无法让文件在服务器上发送和接收。
在网络中我可以看到 POST 请求得到了 200 响应,但是它返回了 'file not present' 的响应,这是来自 laravel 的响应。
同样在 post 请求中,Request Payload 包含以下内容
------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";
}
【问题讨论】:
-
无法通过 ajax 请求发送文件,可以使用 iframe。
-
@marko 你确定吗? - stackoverflow.com/questions/5392344/…
标签: javascript php jquery ajax laravel