我在使用Laravel框架。如何使用ajax上傳文件圖像?
我必須使用Ajax來上傳文件映像,但它的響應爲Call to a member function getClientOriginalExtension() on null
。
表
<form action="{{ route('agent.hotel.image.gallery') }}" enctype="multipart/form-data" method="post" id="form_upload">
{{ csrf_field() }}
<input type="file" name="image_gallery" id="image">
</form>
腳本
$("#image").on('change', function() {
var image = $(this).val();
$.ajax({
method: 'POST',
url: '{{ route('agent.hotel.image.gallery') }}',
data: { image_gallery: image, '_token': '{{ csrf_token() }}' },
success: function (data) {
console.log(data);
}
});
});
控制器
// Image Gallery
$image = $request->file('image_gallery');
$filename = md5(time()) . '.' . $image->getClientOriginalExtension();
$save = public_path('/hotel_picture/' . $filename);
//Move file upload to public folder
Image::make($image)->save($save);
任何人都可以幫助嗎?或者有其他方法來做到這一點?
'.VAL()'返回文件的假路徑,而不是'File'對象,可以使用''元素的'.files'屬性來獲得'File'對象。您的服務器似乎期望''image_gallery'','File'對象的key,value對' – guest271314
您可能必須使用FormData()傳遞圖像,通過ajax – Rasclatt