如何使用Ajax将多个文件值传递给laravel中的控制器?
这就是我想要做的
刀片
<input id="attachment" name="attachment[]" type="file" class="form-control col-md-6" multiple>
<button type="submit" class="btn btn-primary"><i class="fa fa-plus"></i></button>
JS
$(document).on('click', '#addAttachment', function(e)
{
e.preventDefault();
e.stopPropagation();
$.ajax
({
type: 'POST',
url: '/inventory/attach/'+$('input[name=id]').val(),
data: {
'_token' : $('input[name=_token]').val(),
'attachment' : $('input[name=attachment]').val(),
},
success: function(attachment)
{
var attach = '<a href="/attachment/asset/'+attachment.attachment+'"><span class="fas fa-download" style="font-size: 18px"></span>'+attachment.attachment+'</a>'
$('#div_attachment a').remove();
$('#div_attachment').append(attach);
swal({
title: 'Successful!',
text: 'This will automatically close in 2 seconds',
icon: 'success',
timer: 2000,
});
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
swal({
title: 'Error!',
text: 'Please check your connection or contact the administrator!',
icon: 'error',
});
}
});
});
它没有传递控制器上的文件值,而只是给了我一个空值。我还尝试将[]
放入此$('input[name=attachment]').val()
,但它给我一个错误。
任何帮助将不胜感激。谢谢。