Skip to content

Commit 2c5c289

Browse files
author
Martin Kluska
authored
Merge pull request #8 from pionl/pr/7
Add support for Request with multiple files array
2 parents 7d6578c + eac9a50 commit 2c5c289

File tree

3 files changed

+69
-8
lines changed

3 files changed

+69
-8
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
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

src/Receiver/FileReceiver.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,16 @@ class FileReceiver
4545
/**
4646
* The file receiver for the given file index
4747
*
48-
* @param string $fileIndex the desired file index in requests files
49-
* @param Request $request the current request
50-
* @param string $handlerClass the handler class name for detecting the file upload
51-
* @param ChunkStorage|null $chunkStorage the chunk storage, on null will use the instance from app container
52-
* @param AbstractConfig|null $config the config, on null will use the instance from app container
48+
* @param string|UploadedFile $fileIndexOrFile the desired file index to use in request or the final UploadedFile
49+
* @param Request $request the current request
50+
* @param string $handlerClass the handler class name for detecting the file upload
51+
* @param ChunkStorage|null $chunkStorage the chunk storage, on null will use the instance from app container
52+
* @param AbstractConfig|null $config the config, on null will use the instance from app container
5353
*/
54-
public function __construct($fileIndex, Request $request, $handlerClass, $chunkStorage = null, $config = null)
54+
public function __construct($fileIndexOrFile, Request $request, $handlerClass, $chunkStorage = null, $config = null)
5555
{
5656
$this->request = $request;
57-
$this->file = $request->file($fileIndex);
57+
$this->file = is_object($fileIndexOrFile) ? $fileIndexOrFile : $request->file($fileIndexOrFile);
5858
$this->chunkStorage = is_null($chunkStorage) ? ChunkStorage::storage() : $chunkStorage;
5959
$this->config = is_null($config) ? AbstractConfig::config() : $config;
6060

0 commit comments

Comments
 (0)