annotate src/test/upload.js @ 41:acb730710328

minor
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 06 Nov 2022 17:39:21 -0700
parents 1ac7aee13eca
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
40
1ac7aee13eca test cleanup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1 function upload(input,callback) {
1ac7aee13eca test cleanup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
2 let file = input.files[0];
1ac7aee13eca test cleanup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
3 input.value = null;
1ac7aee13eca test cleanup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
4 let request = new XMLHttpRequest();
1ac7aee13eca test cleanup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
5 let url = 'https://upload.uploadcare.com/base/';
1ac7aee13eca test cleanup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
6 request.open( 'POST', url );
1ac7aee13eca test cleanup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
7 request.onload = function() {
1ac7aee13eca test cleanup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
8 if( request.status !== 200 ) {
1ac7aee13eca test cleanup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
9 let err = 'ajax failed: ' + request.status;
1ac7aee13eca test cleanup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
10 if( request.responseText ) {
1ac7aee13eca test cleanup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
11 err += '\n' + request.responseText;
1ac7aee13eca test cleanup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
12 document.write('<pre>'+request.responseText+'</pre>');
1ac7aee13eca test cleanup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
13 }
1ac7aee13eca test cleanup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
14 console.log(err);
1ac7aee13eca test cleanup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
15 ajax( '/error_log.js', 'err='+encodeURIComponent(err) );
1ac7aee13eca test cleanup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
16 return;
1ac7aee13eca test cleanup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
17 }
1ac7aee13eca test cleanup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
18 let response = JSON.parse(request.responseText);
1ac7aee13eca test cleanup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
19 let filename = file.name;
1ac7aee13eca test cleanup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
20 let url = 'https://ucarecdn.com/' + response.file + '/' + filename;
1ac7aee13eca test cleanup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
21 callback(input,url,filename);
1ac7aee13eca test cleanup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
22 };
1ac7aee13eca test cleanup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
23 let formData = new FormData();
1ac7aee13eca test cleanup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
24 formData.append( 'UPLOADCARE_PUB_KEY', 'fe3d30f3088a50941d45' );
1ac7aee13eca test cleanup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
25 formData.append( 'file', file );
1ac7aee13eca test cleanup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
26 request.send(formData);
1ac7aee13eca test cleanup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
27 }
1ac7aee13eca test cleanup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
28
1ac7aee13eca test cleanup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
29 function fileButtonClick(button) {
1ac7aee13eca test cleanup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
30 button.parentNode.querySelector('input[type="file"]').click();
1ac7aee13eca test cleanup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
31 }