|
| 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 | +``` |
0 commit comments