|
| 1 | +# Laravel chunked upload |
| 2 | + |
| 3 | +___Project in progress___ |
| 4 | + |
| 5 | +## Instalation |
| 6 | + |
| 7 | + composer require pion/laravel-chunk-upload |
| 8 | + |
| 9 | +## Usage |
| 10 | + |
| 11 | +In your own controller create the `FileReceiver`, more in example |
| 12 | + |
| 13 | +## Supports |
| 14 | + |
| 15 | +* Laravel 5+ |
| 16 | +* [blueimp-file-upload](https://github.com/blueimp/jQuery-File-Upload) - partial support (simple chunked and single upload) |
| 17 | + |
| 18 | +## Example |
| 19 | + |
| 20 | +### Javascript |
| 21 | + |
| 22 | + $element.fileupload({ |
| 23 | + url: "upload_url", |
| 24 | + maxChunkSize: 1000000, |
| 25 | + method: "POST", |
| 26 | + sequentialUploads: true, |
| 27 | + formData: function(form) { |
| 28 | + //laravel token for communication |
| 29 | + return [{name: "_token", value: $form.find("[name=_token]").val()}]; |
| 30 | + }, |
| 31 | + progressall: function(e, data) { |
| 32 | + var progress = parseInt(data.loaded / data.total * 100, 10); |
| 33 | + console.log(progress+"%"); |
| 34 | + } |
| 35 | + }) |
| 36 | + .bind('fileuploadchunksend', function (e, data) { |
| 37 | + //console.log("fileuploadchunksend"); |
| 38 | + }) |
| 39 | + .bind('fileuploadchunkdone', function (e, data) { |
| 40 | + //console.log("fileuploadchunkdone"); |
| 41 | + }) |
| 42 | + .bind('fileuploadchunkfail', function (e, data) { |
| 43 | + console.log("fileuploadchunkfail") |
| 44 | + }); |
| 45 | + |
| 46 | +### Laravel controller |
| 47 | +Create laravel controller `UploadController` and create the file receiver with the desired handler. |
| 48 | + |
| 49 | +#### Controller |
| 50 | +You must import the full namespace in your controler (`use`). |
| 51 | + |
| 52 | + /** |
| 53 | + * Handles the file upload |
| 54 | + * |
| 55 | + * @param Request $request |
| 56 | + * |
| 57 | + * @return \Illuminate\Http\JsonResponse |
| 58 | + * |
| 59 | + * @throws UploadMissingFileException |
| 60 | + */ |
| 61 | + public function upload(Request $request) { |
| 62 | + |
| 63 | + // create the file receiver |
| 64 | + $receiver = new FileReceiver("file", $request, ContentRangeUploadHandler::class); |
| 65 | + |
| 66 | + // check if the upload is success |
| 67 | + if ($receiver->isUploaded()) { |
| 68 | + |
| 69 | + // receive the file |
| 70 | + $save = $receiver->receive(); |
| 71 | + |
| 72 | + // check if the upload has finished (in chunk mode it will send smaller files) |
| 73 | + if ($save->isFinished()) { |
| 74 | + // save the file and return any response you need |
| 75 | + return $this->saveFile($save->getFile()); |
| 76 | + } else { |
| 77 | + // we are in chunk mode, lets send the current progress |
| 78 | + |
| 79 | + /** @var ContentRangeUploadHandler $handler */ |
| 80 | + $handler = $save->handler(); |
| 81 | + |
| 82 | + return response()->json([ |
| 83 | + "start" => $handler->getBytesStart(), |
| 84 | + "end" => $handler->getBytesEnd(), |
| 85 | + "total" => $handler->getBytesTotal() |
| 86 | + ]); |
| 87 | + } |
| 88 | + } else { |
| 89 | + throw new UploadMissingFileException(); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | +#### Route |
| 94 | +Add a route to your controller |
| 95 | + |
| 96 | + Route::post('upload', 'UploadController@upload'); |
| 97 | + |
| 98 | +## Todo |
| 99 | + |
| 100 | +- [ ] add more providers (like pbupload) |
| 101 | +- [ ] add facade for a quick usage with callback and custom response based on the handler |
| 102 | +- [ ] cron to delete uncompleted files |
| 103 | +- [ ] file per session (to support multiple) |
| 104 | +- [ ] add a config with custom storage location |
| 105 | + |
| 106 | +## Contribution |
| 107 | +Are welcome. To add a new provider, just add a new Handler (which extends AbstractHandler), implement the chunk |
| 108 | +upload and progress |
0 commit comments