Skip to content

Instantly share code, notes, and snippets.

@shrestharikesh
Last active September 1, 2020 06:21
Show Gist options
  • Save shrestharikesh/d89ced5a269d2fe1e17b54913090f2d0 to your computer and use it in GitHub Desktop.
Save shrestharikesh/d89ced5a269d2fe1e17b54913090f2d0 to your computer and use it in GitHub Desktop.
Jquery asynchronous Image Upload on Laravel
<div class="form-group col-md-12">
<label for="fileupload">Album Images</label>
<input type="file" id="fileupload" name="photos[]" data-url="/image/upload" multiple="">
<br>
<div id="files_list"></div>
<p id="loading"></p>
<input type="hidden" name="file_ids" id="file_ids" value="">
</div>
class FileUploadController extends Controller
{
public function imageUpload(Request $request)
{
$photos = [];
foreach ($request->photos as $photo) {
$photos[] = getImageURL($photo->store('public/images'));
}
return response()->json(['files' => $photos], 200);
}
}
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="{{asset('admin/js/fileupload/jquery.ui.widget.js')}}"></script>
<script src="{{asset('admin/js/fileupload/jquery.iframe-transport.js')}}"></script>
<script src="{{asset('admin/js/fileupload/jquery.fileupload.js')}}"></script>
<script>
$(function () {
$('#fileupload').fileupload({
dataType: 'json',
add: function (e, data) {
$('#loading').text('Uploading...');
data.submit();
},
done: function (e, data) {
$.each(data.result.files, function (index,file) {
$('<img />')
.attr('src', file)
.width(200)
.appendTo($('#files_list'));
});
$('#loading').text('');
}
});
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment