|
79 | 79 | useWebWorker: useWebWorker,
|
80 | 80 | onProgress: onProgress
|
81 | 81 | }
|
82 |
| - imageCompression(file, options).then(function (output) { |
83 |
| - logDom.innerHTML += ', output size:' + (output.size / 1024 / 1024).toFixed(2) + 'mb' |
84 |
| - console.log('output', output) |
85 |
| - const downloadLink = URL.createObjectURL(output) |
86 |
| - logDom.innerHTML += ' <a href="' + downloadLink + '" download="' + file.name + '">download compressed image</a>' |
87 |
| - document.getElementById('preview-after-compress').src = downloadLink |
88 |
| - // uploadToServer(output) |
89 |
| - }) |
90 |
| - |
91 |
| - |
92 |
| - |
| 82 | + imageCompression(file, options) |
| 83 | + .then(function (output) { |
| 84 | + logDom.innerHTML += ', output size:' + (output.size / 1024 / 1024).toFixed(2) + 'mb' |
| 85 | + console.log('output', output) |
| 86 | + const downloadLink = URL.createObjectURL(output) |
| 87 | + logDom.innerHTML += ' <a href="' + downloadLink + '" download="' + file.name + '">download compressed image</a>' |
| 88 | + document.getElementById('preview-after-compress').src = downloadLink |
| 89 | + return uploadToServer(output) |
| 90 | + }) |
| 91 | + |
93 | 92 | function onProgress (p) {
|
94 | 93 | console.log('onProgress', p)
|
95 | 94 | progressDom.innerHTML = '(' + p + '%' + ')'
|
96 | 95 | }
|
97 | 96 | }
|
98 | 97 |
|
99 | 98 | function uploadToServer (file) {
|
100 |
| - var formData = new FormData() |
| 99 | + const formData = new FormData() |
101 | 100 | formData.append('image', file, file.name)
|
102 |
| - return fetch('http://localhost/image-upload-api', { |
| 101 | + const url = 'http://localhost:3000/image-upload-api' |
| 102 | + console.log('calling api', url, 'with data', Array.from(formData.entries())[0]) |
| 103 | + return fetch(url, { |
103 | 104 | method: 'POST',
|
104 | 105 | body: formData
|
105 |
| - }) |
| 106 | + }).then(res => res.json()) |
| 107 | + .then(body => console.log('got server response', body)) |
106 | 108 | }
|
107 | 109 | </script>
|
108 | 110 | </body>
|
|
0 commit comments