Created
February 27, 2015 07:46
-
-
Save goshmx/7bb8dc341e4ed73a463f to your computer and use it in GitHub Desktop.
Snippet para subir archivos via AJAX a laravel.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script> | |
//Enviar el formulario via FormData | |
var data = new FormData($('#form-nuevo')[0]); | |
console.log(data); | |
$.ajax( { | |
url: myApp.url.rest+'sistema/imagen', | |
type: 'POST', | |
data: data, | |
processData: false, | |
contentType: false | |
}).done(function(datos){ | |
console.log(datos); | |
}); | |
</script> | |
<?php | |
//Recuerda | |
Route::post('/upload_file', function() | |
{ | |
$rules = array( | |
'file' => 'required|mimes:doc,docx,pdf', | |
); | |
$validator = Validator::make(Input::all(), $rules); | |
if($validator->fails()) | |
{ | |
//Enviar errores | |
} | |
else | |
{ | |
if(Input::hasFile('file')) | |
{ | |
$f = Input::file('file'); | |
$att = new Attachment; | |
$att->name = $f->getClientOriginalName(); | |
$att->file = base64_encode(file_get_contents($f->getRealPath())); | |
$att->mime = $f->getMimeType(); | |
$att->size = $f->getSize(); | |
$att->save(); | |
//Return success | |
} | |
} | |
}); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment