Laravel multiple file upload validation for extensions xls, doc, csv, docx, xlsx etc
I have tried many ways for validating xls and csv file uploaded using larvel from a multiple file upload field and all are not working in my case. Finally I created my own from the search results I found.
$allowedExtensions = ['jpg','jpeg','png','bmp','pdf','doc','xls','xlsx','docx','csv'];
if (Request::hasFile('image')) {
$files = Request::file('image');
foreach($files as $file){
$extension = strtolower($file->getClientOriginalExtension());
if(!in_array($extension, $allowedExtensions)) {
$errors = array('image' => ['Only jpeg, png, bmp, doc,docx, xls,xlsx, csv and pdf files are allowed']);
return new JsonResponse($errors, 422);
}
}
}
I am using the following jquery for submitting the form and showing the errors.
$(document).ready(function(){
$('#sds_form').on("submit", function(e){
e.preventDefault(); //form will not submitted
var route_url = $("#sdsurl").val();
$.ajax({
url: route_url,
method:"POST",
data:new FormData(this),
dataType: "json",
contentType: false,
headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
timeout: 100000, // The content type used when sending data to the server.
cache:false, // To unable request pages to be cached
processData:false, // To send DOMDocument or non processed data file it is set to false
success: function(result){
//indicate successfull message
$("#alter_notify").css('display', 'block');
},
error:function(error){
var obj = $.parseJSON(error.responseText);
var errors = '';
for (var property1 in obj) {
if(typeof(obj[property1][0])!="undefined"){
errors = obj[property1][0];
}
}
$("#image-error").fadeIn("slow").fadeOut(10000);
$("#image-error").html(errors);
$("#alter_submit").html('<i class="fa fa-send"></i> Submit');
}
})
});
});
I have tried many ways for validating xls and csv file uploaded using larvel from a multiple file upload field and all are not working in my case. Finally I created my own from the search results I found.
$allowedExtensions = ['jpg','jpeg','png','bmp','pdf','doc','xls','xlsx','docx','csv'];
if (Request::hasFile('image')) {
$files = Request::file('image');
foreach($files as $file){
$extension = strtolower($file->getClientOriginalExtension());
if(!in_array($extension, $allowedExtensions)) {
$errors = array('image' => ['Only jpeg, png, bmp, doc,docx, xls,xlsx, csv and pdf files are allowed']);
return new JsonResponse($errors, 422);
}
}
}
I am using the following jquery for submitting the form and showing the errors.
$(document).ready(function(){
$('#sds_form').on("submit", function(e){
e.preventDefault(); //form will not submitted
var route_url = $("#sdsurl").val();
$.ajax({
url: route_url,
method:"POST",
data:new FormData(this),
dataType: "json",
contentType: false,
headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
timeout: 100000, // The content type used when sending data to the server.
cache:false, // To unable request pages to be cached
processData:false, // To send DOMDocument or non processed data file it is set to false
success: function(result){
//indicate successfull message
$("#alter_notify").css('display', 'block');
},
error:function(error){
var obj = $.parseJSON(error.responseText);
var errors = '';
for (var property1 in obj) {
if(typeof(obj[property1][0])!="undefined"){
errors = obj[property1][0];
}
}
$("#image-error").fadeIn("slow").fadeOut(10000);
$("#image-error").html(errors);
$("#alter_submit").html('<i class="fa fa-send"></i> Submit');
}
})
});
});