@@ -19,81 +19,86 @@ In your own controller create the `FileReceiver`, more in example
1919
2020### Javascript
2121
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- }
22+ ``` javascript
23+ $element .fileupload ({
24+ url: " upload_url" ,
25+ maxChunkSize: 1000000 ,
26+ method: " POST" ,
27+ sequentialUploads: true ,
28+ formData : function (form ) {
29+ // laravel token for communication
30+ return [{name: " _token" , value: $form .find (" [name=_token]" ).val ()}];
31+ },
32+ progressall : function (e , data ) {
33+ var progress = parseInt (data .loaded / data .total * 100 , 10 );
34+ console .log (progress+ " %" );
35+ }
36+ })
37+ .bind (' fileuploadchunksend' , function (e , data ) {
38+ // console.log("fileuploadchunksend");
3539 })
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-
40+ .bind (' fileuploadchunkdone' , function (e , data ) {
41+ // console.log("fileuploadchunkdone");
42+ })
43+ .bind (' fileuploadchunkfail' , function (e , data ) {
44+ console .log (" fileuploadchunkfail" )
45+ });
46+ ```
47+
4648### Laravel controller
4749Create laravel controller ` UploadController ` and create the file receiver with the desired handler.
4850
4951#### Controller
5052You must import the full namespace in your controler (` use ` ).
5153
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- }
54+ ``` php
55+ /**
56+ * Handles the file upload
57+ *
58+ * @param Request $request
59+ *
60+ * @return \Illuminate\Http\JsonResponse
61+ *
62+ * @throws UploadMissingFileException
63+ */
64+ public function upload(Request $request) {
65+
66+ // create the file receiver
67+ $receiver = new FileReceiver("file", $request, ContentRangeUploadHandler::class);
68+
69+ // check if the upload is success
70+ if ($receiver->isUploaded()) {
71+
72+ // receive the file
73+ $save = $receiver->receive();
74+
75+ // check if the upload has finished (in chunk mode it will send smaller files)
76+ if ($save->isFinished()) {
77+ // save the file and return any response you need
78+ return $this->saveFile($save->getFile());
8879 } else {
89- throw new UploadMissingFileException();
80+ // we are in chunk mode, lets send the current progress
81+
82+ /** @var ContentRangeUploadHandler $handler */
83+ $handler = $save->handler();
84+
85+ return response()->json([
86+ "start" => $handler->getBytesStart(),
87+ "end" => $handler->getBytesEnd(),
88+ "total" => $handler->getBytesTotal()
89+ ]);
9090 }
91+ } else {
92+ throw new UploadMissingFileException();
9193 }
92-
94+ }
95+ ```
9396#### Route
9497Add a route to your controller
9598
96- Route::post('upload', 'UploadController@upload');
99+ ``` php
100+ Route::post('upload', 'UploadController@upload');
101+ ```
97102
98103## Todo
99104
0 commit comments