[英]Upload image AJAX - Laravel 5.2
Hello i'm trying to upload images to Laravel from AJAX using FormData() method. 您好,我正在尝试使用FormData()方法将图像从AJAX上传到Laravel。 I think i'm sending good the file because if I send a file that excess the size (2mb) the server is answering me with validation error but when I send with good image properties its not saving it, I don't know why and I'm not getting any error in console, the status code is 200 OK.
我认为我发送的文件很好,因为如果我发送的文件超过了大小(2mb),则服务器会以验证错误回答我,但是当我发送具有良好图像属性的文件却没有保存时,我不知道为什么,并且我在控制台中没有收到任何错误,状态代码为200 OK。
I already look for another post but didnt work :/ 我已经在找另一篇文章了,但是没有用:/
this is part of my JS code: 这是我的JS代码的一部分:
$(document).ready(function(e) {
$("#uploadimage").on('submit', (function(e) {
e.preventDefault()
$('#mensaje').empty()
$('#loading').show()
var token = $('#token').val()
$.ajax({
url: '/admin/imagenes',
headers: {
"X-CSRF-TOKEN": token
},
type: 'POST',
data: ({
type: 'post',
formData: new FormData(this)
}),
contentType: false,
cache: false,
processData: false,
success: function(data) {
$('#loading').hide()
$('#message').html(data)
}
and this is my Laravel code: 这是我的Laravel代码:
public function store(Request $request) {
if ($request->file('file')) {
$file = $request->file('file');
$name = 'NacionGrita_' .time(). '.' . $file->getClientOriginalExtension();
$path = base_path(). '/imagenes/articulos';
$file->move($path, $name);
$imagen = new Imagen();
$imagen->url = $name;
$imagen->save();
return response()->json(
$file->toArray()
);
}
}
And this my Blade code: 这是我的Blade代码:
{!!Form::open(['route' => 'admin.imagenes.store', 'id' => 'uploadimage', 'method' => 'POST', 'files' => true])!!}
<div class="form-group ">
{!!Form::label('file', 'Imagen principal')!!}
{!!Form::file('file', null, ['class' => 'form-control', 'id' => 'file','placeholder' => 'Ingrese el título del post', 'required'])!!}
</div>
<div class="form-group ">
{!!Form::submit('Agregar Artículo', ['class' => 'btn btn-primary submit', 'id' => 'upImg'])!!}
</div>
{!!Form::close()!!}
</div>
I hope anyone can help me. 我希望有人能帮助我。 Thank you so much.
非常感谢。
I recommend you to implement this plugin: http://malsup.com/jquery/form/#file-upload 我建议您实施此插件: http : //malsup.com/jquery/form/#file-upload
It allows you to send files via Ajax with jquery. 它允许您通过带有jquery的Ajax发送文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.