Skip to content

Commit eac9a50

Browse files
author
Martin Kluska
committed
Updated read-me with example of multiple files in one request #7
1 parent 1910c08 commit eac9a50

File tree

2 files changed

+62
-2
lines changed

2 files changed

+62
-2
lines changed

.gitignore

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1-
# Created by .ignore support plugin (hsz.mobi)
1+
2+
composer.phar
3+
/vendor/
4+
composer.lock
5+

readme.md

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,62 @@ public function upload(Request $request) {
214214
}
215215
```
216216

217+
##### Usage with multiple files in one Request
218+
219+
```php
220+
/**
221+
* Handles the file upload
222+
*
223+
* @param Request $request
224+
*
225+
* @param Int $fileIndex
226+
*
227+
* @return \Illuminate\Http\JsonResponse
228+
*
229+
* @throws UploadMissingFileException
230+
*/
231+
public function upload(Request $request) {
232+
233+
// Response for the files - completed and uncompleted
234+
$files = [];
235+
236+
// Get array of files from request
237+
$files = $request->file('files');
238+
239+
if (!is_array($files)) {
240+
throw new UploadMissingFileException();
241+
}
242+
243+
// Loop sent files
244+
foreach ($files as $file) {
245+
if ($receiver->isUploaded()) {
246+
// receive the file
247+
$save = $receiver->receive();
248+
249+
// check if the upload has finished (in chunk mode it will send smaller files)
250+
if ($save->isFinished()) {
251+
// save the file and return any response you need
252+
$files[] = $this->saveFile($save->getFile());
253+
} else {
254+
// we are in chunk mode, lets send the current progress
255+
256+
/** @var ContentRangeUploadHandler $handler */
257+
$handler = $save->handler();
258+
259+
// Add the completed file
260+
$files[] = [
261+
"start" => $handler->getBytesStart(),
262+
"end" => $handler->getBytesEnd(),
263+
"total" => $handler->getBytesTotal(),
264+
"finished" => false
265+
];
266+
}
267+
}
268+
}
269+
270+
return response()->json($files);
271+
}
272+
```
217273

218274
#### Route
219275
Add a route to your controller
@@ -334,4 +390,4 @@ See [CONTRIBUTING.md](CONTRIBUTING.md) for how to contribute changes. All contri
334390
was written by [Martin Kluska](http://kluska.cz) and is released under the
335391
[MIT License](LICENSE.md).
336392

337-
Copyright (c) 2016 Martin Kluska
393+
Copyright (c) 2016 Martin Kluska

0 commit comments

Comments
 (0)