@@ -243,37 +243,24 @@ async function uploadPhoto() {
243243}
244244
245245function uploadFile ( file , x , y , callback ) {
246- const xhr = new XMLHttpRequest ( ) ;
247246 const formData = new FormData ( ) ;
248247 formData . append ( 'image' , file ) ;
249248 formData . append ( 'imageX' , x ) ;
250249 formData . append ( 'imageY' , y ) ;
251250
252251 try {
253- xhr . open ( 'POST' , '/upload/' ) ;
254-
255- // Upload progress
256- // xhr.upload.addEventListener('progress', (e) => {
257- // if (e.lengthComputable) {
258- // const percent = (e.loaded / e.total) * 100;
259- // console.log('Upload progress:', percent.toFixed(2));
260- // document.documentElement.style.setProperty('--upload-progress', `${percent.toFixed(2)}%`);
261- // }
262- // });
263-
264- xhr . onerror = ( ) => {
265- callback ( { success : false , error : xhr . statusText } ) ;
266- } ;
267-
268- xhr . onload = ( ) => {
269- if ( xhr . status === 200 ) {
270- callback ( { success : true , data : JSON . parse ( xhr . response ) } ) ;
252+ fetch ( '/upload/' , {
253+ method : 'POST' ,
254+ body : formData ,
255+ } ) . then ( response => response . json ( ) ) . then ( data => {
256+ if ( data . success ) {
257+ callback ( { success : true , data : data } ) ;
271258 } else {
272- callback ( { success : false , error : xhr . statusText } ) ;
259+ callback ( { success : false , error : data . error } ) ;
273260 }
274- } ;
275-
276- xhr . send ( formData ) ;
261+ } ) . catch ( error => {
262+ callback ( { success : false , error : error . message } ) ;
263+ } ) ;
277264 } catch ( err ) {
278265 callback ( { success : false , error : err . message } ) ;
279266 }
0 commit comments