我是 laravel 5 和 php 的新手,所以我不明白为什么会遇到这个问题。
我正在尝试使用ajax上传文件(视频或图像),但服务器返回空结果。即使当我控制台 FormData 时,表单看起来还是空的。
HTML
<form enctype="multipart/form-data" id="upload-file" role="form" method="POST" action="" >
<input type="hidden" name="_token" value="{{ csrf_token()}}">
<input type="file" class="form-control" id="catagry-logo" name="videologo">
</form>
<div id="video-submit">Submit</div>
Javascript
$.ajaxSetup({
headers: {
'X-CSRF-Token': $('meta[name=_token]').attr('content')
}
});
$('#video-submit').on('click', function() {
console.log(new FormData($("#upload-file")[0]));
$.ajax({
type:'post',
url:'uploadvideo',
data: new FormData($("#upload_form")[0]),
processData: false,
contentType: false,
success:function(response){
console.log(response);
}
});
});
路线
Route::post('/uploadvideo', 'UploadController@videoUpload');
Controller
public function videoUpload() {
return response()->json(json_encode(Input::all()));
}
回应
FormData {}
[]
最佳答案
您似乎正在使用Input
Facade
尝试熟悉 Request
类
而且,您返回 JSON 响应的方式似乎错误,因此请尝试此操作。
NOTE
Make sure that the PHP Post Upload size in your INI file can handle the file size of your uploads or you will end up with blank POST requests
On your
php.ini
file set the following directives to accomodate your uploads
post_max_size
= (size in mb, the total size of the POST request)upload_max_filesize
= (size in mb, size of a single file in a POST request)
Controller
use Illuminate\Http\Requests;
public function videoUpload( Request $request ) {
return response()->json( ['video' => $request->input( 'videologo' )] );
}
关于javascript - 文件上传返回空结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40901295/