|
| 1 | +--- |
| 2 | +description: Laravel RESTful api or ajax file upload |
| 3 | +keywords: laravel, php, file , restapi |
| 4 | +title: Laravel RESTful api file upload |
| 5 | +toc_max: 4 |
| 6 | +--- |
| 7 | +## RESTful api image upload |
| 8 | + |
| 9 | +## single image upload |
| 10 | + |
| 11 | +* html |
| 12 | + |
| 13 | +```html |
| 14 | +<form class="" id="image-upload-form"> |
| 15 | + <input type='file' name="select_file" id="myfile" /> |
| 16 | + <button type="submit" name="button">submit</button> |
| 17 | +</form> |
| 18 | +``` |
| 19 | + |
| 20 | +* js |
| 21 | + |
| 22 | +```js |
| 23 | +document.getElementById('myfile').addEventListener('change', onSubmit); |
| 24 | + |
| 25 | +function onSubmit(event) { |
| 26 | + event.preventDefault(); |
| 27 | + |
| 28 | + var formData = new FormData(); |
| 29 | + formData.append("file", document.getElementById("myfile").files[0]); |
| 30 | + $.ajax({ |
| 31 | + data: formData, |
| 32 | + url: '{{ route('file.upload.test')}}', |
| 33 | + type: "POST", |
| 34 | + dataType: 'json', |
| 35 | + processData: false, |
| 36 | + contentType: false, |
| 37 | + xhr: function() { |
| 38 | + var xhr = new window.XMLHttpRequest(); |
| 39 | + xhr.upload.addEventListener("progress", function(evt) { |
| 40 | + if (evt.lengthComputable) { |
| 41 | + var percentComplete = (evt.loaded / evt.total) * 100; |
| 42 | + //Do something with upload progress here |
| 43 | + console.log(percentComplete); |
| 44 | + } |
| 45 | + }, false); |
| 46 | + return xhr; |
| 47 | + }, |
| 48 | + success: function (data) { |
| 49 | + console.log(data) |
| 50 | + }, |
| 51 | + error: function (data) { |
| 52 | + //console.log('Error:', data); |
| 53 | + } |
| 54 | + }) |
| 55 | +} |
| 56 | +``` |
| 57 | + |
| 58 | +* laravel php |
| 59 | + |
| 60 | +```php |
| 61 | +use File; |
| 62 | +use Storage; |
| 63 | +use Illuminate\Http\Request; |
| 64 | +class Test extends controllers { |
| 65 | + public function store(Request $request) |
| 66 | + { |
| 67 | + $file = $request->file('file'); |
| 68 | + Storage::disk('local') |
| 69 | + ->put($file->getClientOriginalName(),File::get($file)); |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +``` |
| 74 | + |
| 75 | +## Multiple image upload |
| 76 | + |
| 77 | +* html |
| 78 | + |
| 79 | +```html |
| 80 | +<form> |
| 81 | + <input type='file' name="select_file[]" id="myfile" multiple /> |
| 82 | +</form> |
| 83 | +``` |
| 84 | + |
| 85 | +* js |
| 86 | + |
| 87 | +```js |
| 88 | +document.getElementById('myfile').addEventListener('change', onSubmit); |
| 89 | + |
| 90 | +function onSubmit(event) { |
| 91 | + event.preventDefault(); |
| 92 | + |
| 93 | + var formData = new FormData(); |
| 94 | + var filesLength=document.getElementById('myfile').files.length; |
| 95 | + for(var i=0;i<filesLength;i++){ |
| 96 | + formData.append("files[]", document.getElementById('myfile').files[i]); |
| 97 | + } |
| 98 | + formData.append("msg", 'This is kamal'); |
| 99 | + $.ajax({ |
| 100 | + data: formData, |
| 101 | + url: '{{ route('file.upload.test')}}', |
| 102 | + type: "POST", |
| 103 | + dataType: 'json', |
| 104 | + processData: false, |
| 105 | + contentType: false, |
| 106 | + xhr: function() { |
| 107 | + var xhr = new window.XMLHttpRequest(); |
| 108 | + xhr.upload.addEventListener("progress", function(evt) { |
| 109 | + if (evt.lengthComputable) { |
| 110 | + var percentComplete = (evt.loaded / evt.total) * 100; |
| 111 | + //Do something with upload progress here |
| 112 | + console.log(percentComplete); |
| 113 | + } |
| 114 | + }, false); |
| 115 | + return xhr; |
| 116 | + }, |
| 117 | + success: function (data) { |
| 118 | + console.log(data) |
| 119 | + }, |
| 120 | + error: function (data) { |
| 121 | + //console.log('Error:', data); |
| 122 | + } |
| 123 | + }) |
| 124 | +} |
| 125 | +``` |
| 126 | + |
| 127 | +* laravel php |
| 128 | + |
| 129 | +```php |
| 130 | +use File; |
| 131 | +use Storage; |
| 132 | +use Illuminate\Http\Request; |
| 133 | +class Test extends controllers { |
| 134 | + public function store(Request $request) |
| 135 | + { |
| 136 | + if ($request->hasfile('files')) { |
| 137 | + foreach ($request->file('files') as $file) { |
| 138 | + Storage::disk('local') |
| 139 | + ->put('kamal/'.$file->getClientOriginalName(),File::get($file)); |
| 140 | + } |
| 141 | + } |
| 142 | + } |
| 143 | +} |
| 144 | +``` |
| 145 | + |
| 146 | +## custom storage content route |
| 147 | + |
| 148 | +* if you can't take hassle for symbolic with public folder you can use custom route |
| 149 | + |
| 150 | +* `route` for `local` storage |
| 151 | + |
| 152 | +```php |
| 153 | +Route::get('/storage/{params}', function($params){ |
| 154 | + $path = storage_path('app/'.$params); |
| 155 | + $file = File::get($path); |
| 156 | + $type = File::mimeType($path); |
| 157 | + $response = Response::make($file, 200); |
| 158 | + $response->header("Content-Type", $type); |
| 159 | + if(file_exists(storage_path('app/'.$params))){ |
| 160 | + return $response; |
| 161 | + } |
| 162 | +})->where('params', '.*'); |
| 163 | + |
| 164 | +``` |
| 165 | + |
| 166 | +* `route` for storage `2nd method` |
| 167 | + |
| 168 | +```php |
| 169 | +Route::get('/storage/{disk}/{params}', function($disk,$params){ |
| 170 | + if (Storage::disk($disk)->exists($params)) { |
| 171 | + $storageFile = Storage::disk($disk)->get($params); |
| 172 | + return response($storageFile, 200) |
| 173 | + ->header('Content-Type', Storage::disk('local') |
| 174 | + ->mimeType($params)); |
| 175 | + } |
| 176 | +})->where('params', '.*'); |
| 177 | + |
| 178 | +``` |
| 179 | + |
| 180 | +```php |
| 181 | +{{ asset('storage/diskname/test.png')` }} |
| 182 | +``` |
0 commit comments