Skip to content

Instantly share code, notes, and snippets.

@goshmx
Created February 27, 2015 07:46
Show Gist options
  • Save goshmx/7bb8dc341e4ed73a463f to your computer and use it in GitHub Desktop.
Save goshmx/7bb8dc341e4ed73a463f to your computer and use it in GitHub Desktop.
Snippet para subir archivos via AJAX a laravel.
<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