comparison src/site.js @ 34:c8d47981c74f

upload using ajax
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 02 Aug 2022 20:46:02 -0600
parents 4fdc4ec0050b
children 1ce75c5ab0f7
comparison
equal deleted inserted replaced
33:4fdc4ec0050b 34:c8d47981c74f
1 1
2 function ajax(url,postData) { 2 function ajax(url,postData) {
3 let request = new XMLHttpRequest(); 3 let request = new XMLHttpRequest();
4 let method = postData ? 'POST' : 'GET'; 4 let method = postData ? 'POST' : 'GET';
5 request.open( method, url ); 5 request.open( method, url );
6 let isMultipart = postData instanceof File; 6 if( postData )
7 if( postData && !isMultipart )
8 request.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded' ); 7 request.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded' );
9 request.onload = function() { 8 request.onload = function() {
10 if( request.status !== 200 ) { 9 if( request.status !== 200 ) {
11 window.console && console.log( 'ajax failed: ' + request.status ); 10 window.console && console.log( 'ajax failed: ' + request.status );
12 if( request.responseText ) 11 if( request.responseText )
13 document.write('<pre>'+request.responseText+'</pre>'); 12 document.write('<pre>'+request.responseText+'</pre>');
14 return; 13 return;
15 } 14 }
16 eval( request.responseText ); 15 eval( request.responseText );
17 }; 16 };
18 if(isMultipart) { 17 request.send(postData);
19 let formData = new FormData();
20 formData.append( 'file', postData );
21 request.send(formData);
22 } else {
23 request.send(postData);
24 }
25 } 18 }
26 19
27 window.onerror = function(msg, url, line, col, error) { 20 window.onerror = function(msg, url, line, col, error) {
28 if (!url) 21 if (!url)
29 return; 22 return;
36 err += '\ncolumn = ' + col; 29 err += '\ncolumn = ' + col;
37 if (error && error.stack) 30 if (error && error.stack)
38 err += '\nstack = ' + error.stack; 31 err += '\nstack = ' + error.stack;
39 ajax( '/error_log.js', 'err='+encodeURIComponent(err) ); 32 ajax( '/error_log.js', 'err='+encodeURIComponent(err) );
40 }; 33 };
34
35 function upload(input,callback) {
36 let file = input.files[0];
37 input.value = null;
38 let request = new XMLHttpRequest();
39 let url = 'https://upload.uploadcare.com/base/';
40 request.open( 'POST', url );
41 request.onload = function() {
42 if( request.status !== 200 ) {
43 let err = 'ajax failed: ' + request.status;
44 if( request.responseText ) {
45 err += '\n' + request.responseText;
46 document.write('<pre>'+request.responseText+'</pre>');
47 }
48 console.log(err);
49 ajax( '/error_log.js', 'err='+encodeURIComponent(err) );
50 return;
51 }
52 let response = JSON.parse(request.responseText);
53 let filename = file.name;
54 let url = 'https://ucarecdn.com/' + response.file + '/' + filename;
55 callback(url,filename);
56 };
57 let formData = new FormData();
58 formData.append( 'UPLOADCARE_PUB_KEY', 'fe3d30f3088a50941d45' );
59 formData.append( 'file', file );
60 request.send(formData);
61 }
62
63 function fileButtonClick(button) {
64 button.parentNode.querySelector('input[type="file"]').click();
65 }