Skip to content

Instantly share code, notes, and snippets.

@daftspunk
Created July 29, 2017 07:52
Show Gist options
  • Save daftspunk/7491eccdb1986c2435f8fe64d9714921 to your computer and use it in GitHub Desktop.
Save daftspunk/7491eccdb1986c2435f8fe64d9714921 to your computer and use it in GitHub Desktop.
Sample AJAX file upload via October CMS
title = "AJAX File Upload"
url = "/test"
layout = "default"
is_hidden = 0
==
<?php
use System\Models\File;
function onSubmit()
{
$validator = Validator::make(
$form = Input::all(), [
'image' => 'required|image',
'size' => 'required|integer|min:0|max:500',
]
);
if ($validator->fails()) {
throw new ValidationException($validator);
}
$file = (new File())->fromPost($form['image']);
return [
'#result' => '<img src="'.$file->getThumb($form['size'], $form['size']).'" />'
];
}
?>
==
<div class="container">
<h2>Image Resize</h2>
<form data-request="onSubmit" data-request-files data-request-flash>
<div class="form-group">
<label>Image</label>
<input type="file" name="image" accept="image/*">
</div>
<div class="form-group">
<label>Size</label>
<input type="integer" name="size" class="form-control" value="200">
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</form>
<h2>Result</h2>
<div id="result">No image uploaded</div>
</div>
@aurukas
Copy link

aurukas commented Sep 2, 2017

what about the JS API? Any example on how it works there? thnx

@ran318
Copy link

ran318 commented Oct 20, 2017

I try this sample, but not work.
It seems like the file not post ?

@kojaktaha
Copy link

kojaktaha commented Sep 2, 2018

I need to do the file upload with JavaScript API of OctoberCMS , instead of using a <form> tag and I keep getting a javascript error (undefined FormData)

@ctben
Copy link

ctben commented Sep 6, 2018

Could you please give me an example of multi-files management which users can manage their files at the Frontend ? (Add/Edit/Delete)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment